Skip to content

Commit

Permalink
refactor: gRPC server to disable reflection when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
daizhenbang_323517 committed Feb 14, 2025
1 parent 15168b5 commit b4fed5e
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions transport/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func StreamInterceptor(in ...grpc.StreamServerInterceptor) ServerOption {
}
}

// DisableReflection disuse reflection.
// By default, it will register gRPC reflection and gRPC admin.
func DisableReflection() ServerOption {
return func(s *Server) {
s.disableReflection = true
}
}

// Options with grpc options.
func Options(opts ...grpc.ServerOption) ServerOption {
return func(s *Server) {
Expand All @@ -123,23 +131,24 @@ func Options(opts ...grpc.ServerOption) ServerOption {
// Server is a gRPC server wrapper.
type Server struct {
*grpc.Server
baseCtx context.Context
tlsConf *tls.Config
lis net.Listener
err error
network string
address string
endpoint *url.URL
timeout time.Duration
middleware matcher.Matcher
streamMiddleware matcher.Matcher
unaryInts []grpc.UnaryServerInterceptor
streamInts []grpc.StreamServerInterceptor
grpcOpts []grpc.ServerOption
health *health.Server
customHealth bool
metadata *apimd.Server
adminClean func()
baseCtx context.Context
tlsConf *tls.Config
lis net.Listener
err error
network string
address string
endpoint *url.URL
timeout time.Duration
middleware matcher.Matcher
streamMiddleware matcher.Matcher
unaryInts []grpc.UnaryServerInterceptor
streamInts []grpc.StreamServerInterceptor
grpcOpts []grpc.ServerOption
health *health.Server
customHealth bool
metadata *apimd.Server
adminClean func()
disableReflection bool
}

// NewServer creates a gRPC server by options.
Expand Down Expand Up @@ -185,7 +194,10 @@ func NewServer(opts ...ServerOption) *Server {
grpc_health_v1.RegisterHealthServer(srv.Server, srv.health)
}
apimd.RegisterMetadataServer(srv.Server, srv.metadata)
reflection.Register(srv.Server)
// reflection register
if !srv.disableReflection {
reflection.Register(srv.Server)
}
// admin register
srv.adminClean, _ = admin.Register(srv.Server)
return srv
Expand Down

0 comments on commit b4fed5e

Please sign in to comment.