|
|
@ -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{} |
|
|
|