fix transport early listening (#2177)

Co-authored-by: chenzhihui <chenzhihui@bilibili.com>
pull/2180/head
Tony Chen 2 years ago committed by GitHub
parent 102391165e
commit c90eab4577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      transport/grpc/server.go
  2. 24
      transport/http/server.go

@ -156,8 +156,6 @@ func NewServer(opts ...ServerOption) *Server {
} }
srv.Server = grpc.NewServer(grpcOpts...) srv.Server = grpc.NewServer(grpcOpts...)
srv.metadata = apimd.NewServer(srv.Server) srv.metadata = apimd.NewServer(srv.Server)
// listen and endpoint
srv.err = srv.listenAndEndpoint()
// internal register // internal register
grpc_health_v1.RegisterHealthServer(srv.Server, srv.health) grpc_health_v1.RegisterHealthServer(srv.Server, srv.health)
apimd.RegisterMetadataServer(srv.Server, srv.metadata) apimd.RegisterMetadataServer(srv.Server, srv.metadata)
@ -169,7 +167,7 @@ func NewServer(opts ...ServerOption) *Server {
// examples: // examples:
// grpc://127.0.0.1:9000?isSecure=false // grpc://127.0.0.1:9000?isSecure=false
func (s *Server) Endpoint() (*url.URL, error) { func (s *Server) Endpoint() (*url.URL, error) {
if s.err != nil { if err := s.listenAndEndpoint(); err != nil {
return nil, s.err return nil, s.err
} }
return s.endpoint, nil return s.endpoint, nil
@ -177,7 +175,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
// Start start the gRPC server. // Start start the gRPC server.
func (s *Server) Start(ctx context.Context) error { func (s *Server) Start(ctx context.Context) error {
if s.err != nil { if err := s.listenAndEndpoint(); err != nil {
return s.err return s.err
} }
s.baseCtx = ctx s.baseCtx = ctx
@ -198,15 +196,17 @@ func (s *Server) listenAndEndpoint() error {
if s.lis == nil { if s.lis == nil {
lis, err := net.Listen(s.network, s.address) lis, err := net.Listen(s.network, s.address)
if err != nil { 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 return err
} }
s.lis = lis s.lis = lis
s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("grpc", s.tlsConf != nil), addr)
} }
addr, err := host.Extract(s.address, s.lis) return s.err
if err != nil {
_ = s.lis.Close()
return err
}
s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("grpc", s.tlsConf != nil), addr)
return nil
} }

@ -227,16 +227,16 @@ func (s *Server) filter() mux.MiddlewareFunc {
// https://127.0.0.1:8000 // https://127.0.0.1:8000
// Legacy: http://127.0.0.1:8000?isSecure=false // Legacy: http://127.0.0.1:8000?isSecure=false
func (s *Server) Endpoint() (*url.URL, error) { func (s *Server) Endpoint() (*url.URL, error) {
if s.err != nil { if err := s.listenAndEndpoint(); err != nil {
return nil, s.err return nil, err
} }
return s.endpoint, nil return s.endpoint, nil
} }
// Start start the HTTP server. // Start start the HTTP server.
func (s *Server) Start(ctx context.Context) error { func (s *Server) Start(ctx context.Context) error {
if s.err != nil { if err := s.listenAndEndpoint(); err != nil {
return s.err return err
} }
s.BaseContext = func(net.Listener) context.Context { s.BaseContext = func(net.Listener) context.Context {
return ctx return ctx
@ -264,15 +264,17 @@ func (s *Server) listenAndEndpoint() error {
if s.lis == nil { if s.lis == nil {
lis, err := net.Listen(s.network, s.address) lis, err := net.Listen(s.network, s.address)
if err != nil { 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 return err
} }
s.lis = lis s.lis = lis
s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("http", s.tlsConf != nil), addr)
} }
addr, err := host.Extract(s.address, s.lis) return s.err
if err != nil {
_ = s.lis.Close()
return err
}
s.endpoint = endpoint.NewEndpoint(endpoint.Scheme("http", s.tlsConf != nil), addr)
return nil
} }

Loading…
Cancel
Save