Skip to content

Commit

Permalink
feat: add cloudrunner.WithGRPCServerOptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odsod committed Aug 3, 2021
1 parent 391274f commit 244e61d
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions grpcserver.go
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}
2 changes: 2 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 244e61d

Please sign in to comment.