github.com/msales/pkg/v3@v3.24.0/grpcx/server.go (about)

     1  package grpcx
     2  
     3  import (
     4  	"net"
     5  
     6  	"google.golang.org/grpc"
     7  )
     8  
     9  // Server represents a GRPC server.
    10  type Server struct {
    11  	listener net.Listener
    12  	srv      *grpc.Server
    13  }
    14  
    15  // NewServer creates a new Server instance.
    16  func NewServer(listener net.Listener, srv *grpc.Server) *Server {
    17  	return &Server{
    18  		listener: listener,
    19  		srv:      srv,
    20  	}
    21  }
    22  
    23  // Serve listens for incoming connections and serves RPC responses.
    24  func (s *Server) Serve() error {
    25  	return s.srv.Serve(s.listener)
    26  }
    27  
    28  // Close closes the listener and frees occupied port.
    29  func (s *Server) Close() error {
    30  	s.srv.GracefulStop() // It also closes the listener.
    31  
    32  	return nil
    33  }