fix(transport/grpc): default base ctx (#1465)

* fix grpc base ctx
pull/1466/head
Tony Chen 3 years ago committed by GitHub
parent 798adbff5e
commit 0184d217cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      transport/grpc/server.go
  2. 2
      transport/grpc/server_test.go

@ -92,7 +92,7 @@ func Options(opts ...grpc.ServerOption) ServerOption {
// Server is a gRPC server wrapper. // Server is a gRPC server wrapper.
type Server struct { type Server struct {
*grpc.Server *grpc.Server
ctx context.Context baseCtx context.Context
tlsConf *tls.Config tlsConf *tls.Config
lis net.Listener lis net.Listener
once sync.Once once sync.Once
@ -112,6 +112,7 @@ type Server struct {
// NewServer creates a gRPC server by options. // NewServer creates a gRPC server by options.
func NewServer(opts ...ServerOption) *Server { func NewServer(opts ...ServerOption) *Server {
srv := &Server{ srv := &Server{
baseCtx: context.Background(),
network: "tcp", network: "tcp",
address: ":0", address: ":0",
timeout: 1 * time.Second, timeout: 1 * time.Second,
@ -175,7 +176,7 @@ func (s *Server) Start(ctx context.Context) error {
if _, err := s.Endpoint(); err != nil { if _, err := s.Endpoint(); err != nil {
return err return err
} }
s.ctx = ctx s.baseCtx = ctx
s.log.Infof("[gRPC] server listening on: %s", s.lis.Addr().String()) s.log.Infof("[gRPC] server listening on: %s", s.lis.Addr().String())
s.health.Resume() s.health.Resume()
return s.Serve(s.lis) return s.Serve(s.lis)
@ -191,7 +192,7 @@ func (s *Server) Stop(ctx context.Context) error {
func (s *Server) unaryServerInterceptor() grpc.UnaryServerInterceptor { func (s *Server) unaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, cancel := ic.Merge(ctx, s.ctx) ctx, cancel := ic.Merge(ctx, s.baseCtx)
defer cancel() defer cancel()
md, _ := grpcmd.FromIncomingContext(ctx) md, _ := grpcmd.FromIncomingContext(ctx)
replyHeader := grpcmd.MD{} replyHeader := grpcmd.MD{}

@ -143,7 +143,7 @@ func TestServer_unaryServerInterceptor(t *testing.T) {
u, err := url.Parse("grpc://hello/world") u, err := url.Parse("grpc://hello/world")
assert.NoError(t, err) assert.NoError(t, err)
srv := &Server{ srv := &Server{
ctx: context.Background(), baseCtx: context.Background(),
endpoint: u, endpoint: u,
middleware: []middleware.Middleware{EmptyMiddleware()}, middleware: []middleware.Middleware{EmptyMiddleware()},
timeout: time.Duration(10), timeout: time.Duration(10),

Loading…
Cancel
Save