move gRPC listener creation outside of grpcServer

For tests, we need to create the grpc listener with port 0 in order to
automatically assign a port. This PR moves the lister creation outside
of the grcpServer itself so that we can access that auto-created port.
This commit is contained in:
Travis 2020-04-18 11:38:15 -05:00
parent 72d496f5c7
commit 7de8399b17
4 changed files with 31 additions and 12 deletions

View file

@ -71,6 +71,7 @@ const (
type Node struct {
ID string `json:"id"`
URI URI `json:"uri"`
GRPCURI URI `json:"grpc-uri"`
IsCoordinator bool `json:"isCoordinator"`
State string `json:"state"`
}

View file

@ -70,6 +70,7 @@ type Server struct { // nolint: maligned
nodeID string
uri URI
grpcURI URI
antiEntropyInterval time.Duration
metricInterval time.Duration
diagnosticInterval time.Duration
@ -236,6 +237,15 @@ func OptServerURI(uri *URI) ServerOption {
}
}
// OptServerGRPCURI is a functional option on Server
// used to set the server gRPC URI.
func OptServerGRPCURI(uri *URI) ServerOption {
return func(s *Server) error {
s.grpcURI = *uri
return nil
}
}
// OptServerClusterDisabled tells the server whether to use a static cluster with the
// defined hosts. Mostly used for testing.
func OptServerClusterDisabled(disabled bool, hosts []string) ServerOption {
@ -370,6 +380,7 @@ func NewServer(opts ...ServerOption) (*Server, error) {
node := &Node{
ID: s.nodeID,
URI: s.uri,
GRPCURI: s.grpcURI,
IsCoordinator: s.cluster.Coordinator == s.nodeID,
State: nodeStateDown,
}

View file

@ -736,7 +736,7 @@ func (h grpcHandler) Inspect(req *pb.InspectRequest, stream pb.Pilosa_InspectSer
type grpcServer struct {
api *pilosa.API
grpcServer *grpc.Server
hostPort string
ln net.Listener
logger logger.Logger
stats stats.StatsClient
@ -751,10 +751,9 @@ func OptGRPCServerAPI(api *pilosa.API) grpcServerOption {
}
}
func OptGRPCServerURI(uri *pilosa.URI) grpcServerOption {
hostport := fmt.Sprintf("%s:%d", uri.Host, uri.Port)
func OptGRPCServerListener(ln net.Listener) grpcServerOption {
return func(s *grpcServer) error {
s.hostPort = hostport
s.ln = ln
return nil
}
}
@ -774,12 +773,7 @@ func OptGRPCServerStats(stats stats.StatsClient) grpcServerOption {
}
func (s *grpcServer) Serve(tlsConfig *tls.Config) error {
// create listener
lis, err := net.Listen("tcp", s.hostPort)
if err != nil {
return errors.Wrap(err, "creating listener")
}
s.logger.Printf("enabled grpc listening on %s", lis.Addr())
s.logger.Printf("enabled grpc listening on %s", s.ln.Addr())
opts := make([]grpc.ServerOption, 0)
if tlsConfig != nil {
@ -795,7 +789,7 @@ func (s *grpcServer) Serve(tlsConfig *tls.Config) error {
reflection.Register(s.grpcServer)
// and start...
if err := s.grpcServer.Serve(lis); err != nil {
if err := s.grpcServer.Serve(s.ln); err != nil {
return errors.Wrap(err, "starting grpc server")
}
return nil

View file

@ -82,6 +82,7 @@ type Command struct {
Handler pilosa.Handler
grpcServer *grpcServer
grpcLn net.Listener
API *pilosa.API
ln net.Listener
listenURI *pilosa.URI
@ -270,6 +271,17 @@ func (m *Command) SetupServer() error {
return errors.Wrap(err, "processing bind grpc address")
}
// create gRPC listener
m.grpcLn, err = net.Listen("tcp", grpcURI.HostPort())
if err != nil {
return errors.Wrap(err, "creating grpc listener")
}
// If grpc port is 0, get auto-allocated port from listener
if grpcURI.Port == 0 {
grpcURI.SetPort(uint16(m.grpcLn.Addr().(*net.TCPAddr).Port))
}
// Setup TLS
if uri.Scheme == "https" {
m.tlsConfig, err = GetTLSConfig(&m.Config.TLS, m.logger.Logger())
@ -340,6 +352,7 @@ func (m *Command) SetupServer() error {
pilosa.OptServerGCNotifier(gcnotify.NewActiveGCNotifier()),
pilosa.OptServerStatsClient(statsClient),
pilosa.OptServerURI(advertiseURI),
pilosa.OptServerGRPCURI(grpcURI),
pilosa.OptServerInternalClient(http.NewInternalClientFromURI(uri, c)),
pilosa.OptServerClusterDisabled(m.Config.Cluster.Disabled, m.Config.Cluster.Hosts),
pilosa.OptServerSerializer(proto.Serializer{}),
@ -375,7 +388,7 @@ func (m *Command) SetupServer() error {
m.grpcServer, err = NewGRPCServer(
OptGRPCServerAPI(m.API),
OptGRPCServerURI(grpcURI),
OptGRPCServerListener(m.grpcLn),
OptGRPCServerLogger(m.logger),
OptGRPCServerStats(statsClient),
)