Skip to content

Commit

Permalink
added support for interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
erictg committed Apr 25, 2024
1 parent 11de1eb commit 4e776d3
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ type service struct {
}

type runtimeOptions struct {
port uint
logHandler slog.Handler
tc credentials.TransportCredentials
services []service
port uint
logHandler slog.Handler
tc credentials.TransportCredentials
services []service
serverOptions []grpc.ServerOption
}

// RuntimeOption are options for configuring the gRPC runtime.
Expand Down Expand Up @@ -63,6 +64,34 @@ func TransportCredentials(tc credentials.TransportCredentials) RuntimeOption {
}
}

// UnaryInterceptor configures the gRPC server for one unary interceptors, please refer to grpc.UnaryInterceptor for more information
func UnaryInterceptor(f grpc.UnaryServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.UnaryInterceptor(f))
}
}

// ChainUnaryInterceptor configures the gRPC server for multiple unary interceptors, please refer to grpc.ChainUnaryInterceptor for more information
func ChainUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.ChainUnaryInterceptor(interceptors...))
}
}

// StreamInterceptor configures the gRPC server for one server interceptor, please refer to grpc.StreamInterceptor for more information
func StreamInterceptor(i grpc.StreamServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.StreamInterceptor(i))
}
}

// ChainStreamInterceptor configires the gRPC server for multiple server interceptors, please refer to grpc.ChainStreamInterceptor for more information
func ChainStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.ChainStreamInterceptor(interceptors...))
}
}

type serviceOptions struct {
name string
readiness health.Metric
Expand Down Expand Up @@ -127,20 +156,23 @@ type Runtime struct {
// NewRuntime returns a fully initialized gRPC Runtime.
func NewRuntime(opts ...RuntimeOption) *Runtime {
ro := &runtimeOptions{
port: 8090,
logHandler: noop.LogHandler{},
tc: insecure.NewCredentials(),
port: 8090,
logHandler: noop.LogHandler{},
tc: insecure.NewCredentials(),
serverOptions: []grpc.ServerOption{},
}
for _, opt := range opts {
opt(ro)
}

var healthMonitors []serviceHealthMonitor
s := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler(
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
grpc.Creds(ro.tc),
append(ro.serverOptions, []grpc.ServerOption{
grpc.StatsHandler(otelgrpc.NewServerHandler(
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
grpc.Creds(ro.tc),
}...)...,
)
for _, svc := range ro.services {
svc.registerFunc(s)
Expand Down

0 comments on commit 4e776d3

Please sign in to comment.