mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
refactor zrpc server interceptor builder (#4300)
This commit is contained in:
@@ -5,9 +5,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/proc"
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
"github.com/zeromicro/go-zero/internal/health"
|
||||
"github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
)
|
||||
@@ -19,38 +17,31 @@ type (
|
||||
ServerOption func(options *rpcServerOptions)
|
||||
|
||||
rpcServerOptions struct {
|
||||
metrics *stat.Metrics
|
||||
health bool
|
||||
health bool
|
||||
}
|
||||
|
||||
rpcServer struct {
|
||||
*baseRpcServer
|
||||
name string
|
||||
middlewares ServerMiddlewaresConf
|
||||
healthManager health.Probe
|
||||
}
|
||||
)
|
||||
|
||||
// NewRpcServer returns a Server.
|
||||
func NewRpcServer(addr string, middlewares ServerMiddlewaresConf, opts ...ServerOption) Server {
|
||||
func NewRpcServer(addr string, opts ...ServerOption) Server {
|
||||
var options rpcServerOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
if options.metrics == nil {
|
||||
options.metrics = stat.NewMetrics(addr)
|
||||
}
|
||||
|
||||
return &rpcServer{
|
||||
baseRpcServer: newBaseRpcServer(addr, &options),
|
||||
middlewares: middlewares,
|
||||
healthManager: health.NewHealthManager(fmt.Sprintf("%s-%s", probeNamePrefix, addr)),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *rpcServer) SetName(name string) {
|
||||
s.name = name
|
||||
s.baseRpcServer.SetName(name)
|
||||
}
|
||||
|
||||
func (s *rpcServer) Start(register RegisterFn) error {
|
||||
@@ -59,8 +50,8 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
||||
return err
|
||||
}
|
||||
|
||||
unaryInterceptorOption := grpc.ChainUnaryInterceptor(s.buildUnaryInterceptors()...)
|
||||
streamInterceptorOption := grpc.ChainStreamInterceptor(s.buildStreamInterceptors()...)
|
||||
unaryInterceptorOption := grpc.ChainUnaryInterceptor(s.unaryInterceptors...)
|
||||
streamInterceptorOption := grpc.ChainStreamInterceptor(s.streamInterceptors...)
|
||||
|
||||
options := append(s.options, unaryInterceptorOption, streamInterceptorOption)
|
||||
server := grpc.NewServer(options...)
|
||||
@@ -87,52 +78,6 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
||||
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 append(interceptors, s.streamInterceptors...)
|
||||
}
|
||||
|
||||
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, s.middlewares.StatConf))
|
||||
}
|
||||
if s.middlewares.Prometheus {
|
||||
interceptors = append(interceptors, serverinterceptors.UnaryPrometheusInterceptor)
|
||||
}
|
||||
if s.middlewares.Breaker {
|
||||
interceptors = append(interceptors, serverinterceptors.UnaryBreakerInterceptor)
|
||||
}
|
||||
|
||||
return append(interceptors, s.unaryInterceptors...)
|
||||
}
|
||||
|
||||
// WithMetrics returns a func that sets metrics to a Server.
|
||||
func WithMetrics(metrics *stat.Metrics) ServerOption {
|
||||
return func(options *rpcServerOptions) {
|
||||
options.metrics = metrics
|
||||
}
|
||||
}
|
||||
|
||||
// WithRpcHealth returns a func that sets rpc health switch to a Server.
|
||||
func WithRpcHealth(health bool) ServerOption {
|
||||
return func(options *rpcServerOptions) {
|
||||
|
||||
Reference in New Issue
Block a user