From 244e61d20934f3a21130559a9f12474aeccffeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20S=C3=B6derlund?= Date: Mon, 2 Aug 2021 21:26:56 +0200 Subject: [PATCH] feat: add cloudrunner.WithGRPCServerOptions For including additional default options to cloudrunner.NewGRPCServer. Intended to be used together with cloudrunner.WithOptions to include additional gRPC server options in an options bundle. --- grpcserver.go | 6 ++++-- options.go | 8 ++++++++ run.go | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/grpcserver.go b/grpcserver.go index 5400d452..a74c6c47 100644 --- a/grpcserver.go +++ b/grpcserver.go @@ -16,7 +16,7 @@ func NewGRPCServer(ctx context.Context, opts ...grpc.ServerOption) *grpc.Server if !ok { panic("cloudrunner.NewGRPCServer: must be called with a context from cloudrunner.Run") } - defaultOpts := []grpc.ServerOption{ + serverOptions := []grpc.ServerOption{ grpc.ChainUnaryInterceptor( otelgrpc.UnaryServerInterceptor(), runCtx.loggerMiddleware.GRPCUnaryServerInterceptor, // adds context logger @@ -25,7 +25,9 @@ func NewGRPCServer(ctx context.Context, opts ...grpc.ServerOption) *grpc.Server runCtx.serverMiddleware.GRPCUnaryServerInterceptor, // needs to run after request logger ), } - return grpc.NewServer(append(defaultOpts, opts...)...) + serverOptions = append(serverOptions, runCtx.grpcServerOptions...) + serverOptions = append(serverOptions, opts...) + return grpc.NewServer(serverOptions...) } // ListenGRPC binds a listener on the configured port and listens for gRPC requests. diff --git a/options.go b/options.go index 09fe3c93..048699de 100644 --- a/options.go +++ b/options.go @@ -2,6 +2,7 @@ package cloudrunner import ( "go.einride.tech/cloudrunner/cloudconfig" + "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) @@ -30,3 +31,10 @@ func WithOptions(options []Option) Option { } } } + +// WithGRPCServerOptions configures the run context with additional default options for NewGRPCServer. +func WithGRPCServerOptions(grpcServerOptions []grpc.ServerOption) Option { + return func(runCtx *runContext) { + runCtx.grpcServerOptions = append(runCtx.grpcServerOptions, grpcServerOptions...) + } +} diff --git a/run.go b/run.go index cc1111ab..6790d0f7 100644 --- a/run.go +++ b/run.go @@ -16,6 +16,7 @@ import ( "go.einride.tech/cloudrunner/cloudtrace" "go.einride.tech/cloudrunner/cloudzap" "go.uber.org/zap" + "google.golang.org/grpc" ) // runConfig configures the Run entrypoint from environment variables. @@ -99,6 +100,7 @@ func Run(run func(context.Context) error, options ...Option) error { type runContext struct { runConfig runConfig configOptions []cloudconfig.Option + grpcServerOptions []grpc.ServerOption loggerMiddleware cloudzap.Middleware serverMiddleware cloudserver.Middleware clientMiddleware cloudclient.Middleware