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

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

Loading…
Cancel
Save