diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-12-21 10:50:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-01-09 10:07:41 +0300 |
commit | b9a84eca418534a7c7796cbff01cb08952fed1c2 (patch) | |
tree | 495c7b396aa1311cffad2d925c1cb9e5798854d8 | |
parent | 1fd56444ffb8ce065b3f9b62670bb048b7a11919 (diff) |
gitaly/server: Allow injecting custom interceptors
We're about to add some testserver-specific interceptors. Add the
infrastructure to add interceptors via options.
-rw-r--r-- | internal/gitaly/server/server.go | 36 | ||||
-rw-r--r-- | internal/gitaly/server/server_factory.go | 8 | ||||
-rw-r--r-- | internal/testhelper/testserver/gitaly.go | 4 |
3 files changed, 40 insertions, 8 deletions
diff --git a/internal/gitaly/server/server.go b/internal/gitaly/server/server.go index bb35a4cae..94f11279b 100644 --- a/internal/gitaly/server/server.go +++ b/internal/gitaly/server/server.go @@ -49,8 +49,35 @@ func init() { grpcmwlogrus.ReplaceGrpcLogger(gitalylog.GrpcGo()) } +type serverConfig struct { + unaryInterceptors []grpc.UnaryServerInterceptor + streamInterceptors []grpc.StreamServerInterceptor +} + +// Option is an option that can be passed to `New()`. +type Option func(*serverConfig) + +// WithUnaryInterceptor adds another interceptor that shall be executed for unary RPC calls. +func WithUnaryInterceptor(interceptor grpc.UnaryServerInterceptor) Option { + return func(cfg *serverConfig) { + cfg.unaryInterceptors = append(cfg.unaryInterceptors, interceptor) + } +} + +// WithStreamInterceptor adds another interceptor that shall be executed for streaming RPC calls. +func WithStreamInterceptor(interceptor grpc.StreamServerInterceptor) Option { + return func(cfg *serverConfig) { + cfg.streamInterceptors = append(cfg.streamInterceptors, interceptor) + } +} + // New returns a GRPC server instance with a set of interceptors configured. -func (s *GitalyServerFactory) New(secure bool) (*grpc.Server, error) { +func (s *GitalyServerFactory) New(secure bool, opts ...Option) (*grpc.Server, error) { + var cfg serverConfig + for _, opt := range opts { + opt(&cfg) + } + ctxTagOpts := []grpcmwtags.Option{ grpcmwtags.WithFieldExtractorForInitialReq(fieldextractors.FieldExtractor), } @@ -140,7 +167,10 @@ func (s *GitalyServerFactory) New(secure bool) (*grpc.Server, error) { panichandler.UnaryPanicHandler, ) - opts := []grpc.ServerOption{ + streamServerInterceptors = append(streamServerInterceptors, cfg.streamInterceptors...) + unaryServerInterceptors = append(unaryServerInterceptors, cfg.unaryInterceptors...) + + serverOptions := []grpc.ServerOption{ grpc.StatsHandler(gitalylog.PerRPCLogHandler{ Underlying: &grpcstats.PayloadBytes{}, FieldProducers: []gitalylog.FieldsProducer{grpcstats.FieldsProducer}, @@ -161,5 +191,5 @@ func (s *GitalyServerFactory) New(secure bool) (*grpc.Server, error) { }), } - return grpc.NewServer(opts...), nil + return grpc.NewServer(serverOptions...), nil } diff --git a/internal/gitaly/server/server_factory.go b/internal/gitaly/server/server_factory.go index 00e2b615d..f9de03235 100644 --- a/internal/gitaly/server/server_factory.go +++ b/internal/gitaly/server/server_factory.go @@ -77,8 +77,8 @@ func (s *GitalyServerFactory) GracefulStop() { // CreateExternal creates a new external gRPC server. The external servers are closed // before the internal servers when gracefully shutting down. -func (s *GitalyServerFactory) CreateExternal(secure bool) (*grpc.Server, error) { - server, err := s.New(secure) +func (s *GitalyServerFactory) CreateExternal(secure bool, opts ...Option) (*grpc.Server, error) { + server, err := s.New(secure, opts...) if err != nil { return nil, err } @@ -89,8 +89,8 @@ func (s *GitalyServerFactory) CreateExternal(secure bool) (*grpc.Server, error) // CreateInternal creates a new internal gRPC server. Internal servers are closed // after the external ones when gracefully shutting down. -func (s *GitalyServerFactory) CreateInternal() (*grpc.Server, error) { - server, err := s.New(false) +func (s *GitalyServerFactory) CreateInternal(opts ...Option) (*grpc.Server, error) { + server, err := s.New(false, opts...) if err != nil { return nil, err } diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.go index 6401cd25b..9ec09d357 100644 --- a/internal/testhelper/testserver/gitaly.go +++ b/internal/testhelper/testserver/gitaly.go @@ -185,7 +185,9 @@ func runGitaly(tb testing.TB, cfg config.Cfg, rubyServer *rubyserver.Server, reg waitHealthy(tb, ctx, "unix://"+internalListener.Addr().String(), cfg.Auth.Token) } - externalServer, err := serverFactory.CreateExternal(cfg.TLS.CertPath != "" && cfg.TLS.KeyPath != "") + secure := cfg.TLS.CertPath != "" && cfg.TLS.KeyPath != "" + + externalServer, err := serverFactory.CreateExternal(secure) require.NoError(tb, err) tb.Cleanup(externalServer.Stop) |