From c90eab45776eb73b46a5ced11caeff0948d6ed44 Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Sun, 10 Jul 2022 15:11:18 +0800 Subject: [PATCH] fix transport early listening (#2177) Co-authored-by: chenzhihui --- transport/grpc/server.go | 22 +++++++++++----------- transport/http/server.go | 24 +++++++++++++----------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/transport/grpc/server.go b/transport/grpc/server.go index 31c03f75e..0131692e3 100644 --- a/transport/grpc/server.go +++ b/transport/grpc/server.go @@ -156,8 +156,6 @@ func NewServer(opts ...ServerOption) *Server { } srv.Server = grpc.NewServer(grpcOpts...) srv.metadata = apimd.NewServer(srv.Server) - // listen and endpoint - srv.err = srv.listenAndEndpoint() // internal register grpc_health_v1.RegisterHealthServer(srv.Server, srv.health) apimd.RegisterMetadataServer(srv.Server, srv.metadata) @@ -169,7 +167,7 @@ func NewServer(opts ...ServerOption) *Server { // examples: // grpc://127.0.0.1:9000?isSecure=false func (s *Server) Endpoint() (*url.URL, error) { - if s.err != nil { + if err := s.listenAndEndpoint(); err != nil { return nil, s.err } return s.endpoint, nil @@ -177,7 +175,7 @@ func (s *Server) Endpoint() (*url.URL, error) { // Start start the gRPC server. func (s *Server) Start(ctx context.Context) error { - if s.err != nil { + if err := s.listenAndEndpoint(); err != nil { return s.err } s.baseCtx = ctx @@ -198,15 +196,17 @@ func (s *Server) listenAndEndpoint() error { if s.lis == nil { lis, err := net.Listen(s.network, s.address) if err != nil { + s.err = err + return err + } + addr, err := host.Extract(s.address, lis) + if err != nil { + _ = s.lis.Close() + s.err = err return err } s.lis = lis + s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("grpc", s.tlsConf != nil), addr) } - addr, err := host.Extract(s.address, s.lis) - if err != nil { - _ = s.lis.Close() - return err - } - s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("grpc", s.tlsConf != nil), addr) - return nil + return s.err } diff --git a/transport/http/server.go b/transport/http/server.go index c45a72697..637ef72ab 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -227,16 +227,16 @@ func (s *Server) filter() mux.MiddlewareFunc { // https://127.0.0.1:8000 // Legacy: http://127.0.0.1:8000?isSecure=false func (s *Server) Endpoint() (*url.URL, error) { - if s.err != nil { - return nil, s.err + if err := s.listenAndEndpoint(); err != nil { + return nil, err } return s.endpoint, nil } // Start start the HTTP server. func (s *Server) Start(ctx context.Context) error { - if s.err != nil { - return s.err + if err := s.listenAndEndpoint(); err != nil { + return err } s.BaseContext = func(net.Listener) context.Context { return ctx @@ -264,15 +264,17 @@ func (s *Server) listenAndEndpoint() error { if s.lis == nil { lis, err := net.Listen(s.network, s.address) if err != nil { + s.err = err + return err + } + addr, err := host.Extract(s.address, lis) + if err != nil { + _ = s.lis.Close() + s.err = err return err } s.lis = lis + s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("http", s.tlsConf != nil), addr) } - addr, err := host.Extract(s.address, s.lis) - if err != nil { - _ = s.lis.Close() - return err - } - s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("http", s.tlsConf != nil), addr) - return nil + return s.err }