|
|
@ -26,12 +26,13 @@ type (
|
|
|
|
rpcServer struct {
|
|
|
|
rpcServer struct {
|
|
|
|
*baseRpcServer
|
|
|
|
*baseRpcServer
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
|
|
|
|
middlewares ServerMiddlewaresConf
|
|
|
|
healthManager health.Probe
|
|
|
|
healthManager health.Probe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// NewRpcServer returns a Server.
|
|
|
|
// NewRpcServer returns a Server.
|
|
|
|
func NewRpcServer(addr string, opts ...ServerOption) Server {
|
|
|
|
func NewRpcServer(addr string, middlewares ServerMiddlewaresConf, opts ...ServerOption) Server {
|
|
|
|
var options rpcServerOptions
|
|
|
|
var options rpcServerOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
|
|
|
opt(&options)
|
|
|
@ -42,6 +43,7 @@ func NewRpcServer(addr string, opts ...ServerOption) Server {
|
|
|
|
|
|
|
|
|
|
|
|
return &rpcServer{
|
|
|
|
return &rpcServer{
|
|
|
|
baseRpcServer: newBaseRpcServer(addr, &options),
|
|
|
|
baseRpcServer: newBaseRpcServer(addr, &options),
|
|
|
|
|
|
|
|
middlewares: middlewares,
|
|
|
|
healthManager: health.NewHealthManager(fmt.Sprintf("%s-%s", probeNamePrefix, addr)),
|
|
|
|
healthManager: health.NewHealthManager(fmt.Sprintf("%s-%s", probeNamePrefix, addr)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -57,19 +59,9 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unaryInterceptors := []grpc.UnaryServerInterceptor{
|
|
|
|
unaryInterceptors := s.buildUnaryInterceptors()
|
|
|
|
serverinterceptors.UnaryTracingInterceptor,
|
|
|
|
|
|
|
|
serverinterceptors.UnaryCrashInterceptor,
|
|
|
|
|
|
|
|
serverinterceptors.UnaryStatInterceptor(s.metrics),
|
|
|
|
|
|
|
|
serverinterceptors.UnaryPrometheusInterceptor,
|
|
|
|
|
|
|
|
serverinterceptors.UnaryBreakerInterceptor,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
|
|
|
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
|
|
|
streamInterceptors := []grpc.StreamServerInterceptor{
|
|
|
|
streamInterceptors := s.buildStreamInterceptors()
|
|
|
|
serverinterceptors.StreamTracingInterceptor,
|
|
|
|
|
|
|
|
serverinterceptors.StreamCrashInterceptor,
|
|
|
|
|
|
|
|
serverinterceptors.StreamBreakerInterceptor,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
|
|
|
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
|
|
|
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
|
|
|
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
|
|
|
WithStreamServerInterceptors(streamInterceptors...))
|
|
|
|
WithStreamServerInterceptors(streamInterceptors...))
|
|
|
@ -97,6 +89,44 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
|
|
|
return server.Serve(lis)
|
|
|
|
return server.Serve(lis)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *rpcServer) buildStreamInterceptors() []grpc.StreamServerInterceptor {
|
|
|
|
|
|
|
|
var interceptors []grpc.StreamServerInterceptor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if s.middlewares.Trace {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.StreamTracingInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Recover {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.StreamRecoverInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Breaker {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.StreamBreakerInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return interceptors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *rpcServer) buildUnaryInterceptors() []grpc.UnaryServerInterceptor {
|
|
|
|
|
|
|
|
var interceptors []grpc.UnaryServerInterceptor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if s.middlewares.Trace {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.UnaryTracingInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Recover {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.UnaryRecoverInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Stat {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.UnaryStatInterceptor(s.metrics))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Prometheus {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.UnaryPrometheusInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.middlewares.Breaker {
|
|
|
|
|
|
|
|
interceptors = append(interceptors, serverinterceptors.UnaryBreakerInterceptor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return interceptors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithMetrics returns a func that sets metrics to a Server.
|
|
|
|
// WithMetrics returns a func that sets metrics to a Server.
|
|
|
|
func WithMetrics(metrics *stat.Metrics) ServerOption {
|
|
|
|
func WithMetrics(metrics *stat.Metrics) ServerOption {
|
|
|
|
return func(options *rpcServerOptions) {
|
|
|
|
return func(options *rpcServerOptions) {
|
|
|
|