Documentation ¶
Overview ¶
Package middleware
`middleware` is a collection of gRPC middleware packages: interceptors, helpers and tools.
Middleware ¶
gRPC is a fantastic RPC middleware, which sees a lot of adoption in the Golang world. However, the upstream gRPC codebase is relatively bare bones.
This package, and most of its child packages provides commonly needed middleware for gRPC: client-side interceptors for retires, server-side interceptors for input validation and auth, functions for chaining said interceptors, metadata convenience methods and more.
Chaining ¶
Simple way of turning a multiple interceptors into a single interceptor. Here's an example for server chaining:
myServer := grpc.NewServer( grpc.ChainStreamInterceptor(loggingStream, monitoringStream, authStream), grpc.ChainUnaryInterceptor(loggingUnary, monitoringUnary, authUnary), )
These interceptors will be executed from left to right: logging, monitoring and auth.
Here's an example for client side chaining:
clientConn, err = grpc.Dial( address, grpc.WithChainUnaryInterceptor(monitoringClientUnary, retryUnary), grpc.WithChainStreamInterceptor(monitoringClientStream, retryStream), ) client = testpb.NewTestServiceClient(clientConn) resp, err := client.PingEmpty(s.ctx, &myservice.Request{Msg: "hello"})
These interceptors will be executed from left to right: monitoring and then retry logic.
The retry interceptor will call every interceptor that follows it whenever when a retry happens.
Writing Your Own ¶
Implementing your own interceptor is pretty trivial: there are interfaces for that. But the interesting bit exposing common data to handlers (and other middleware), similarly to HTTP Middleware design. For example, you may want to pass the identity of the caller from the auth interceptor all the way to the handling function.
For example, a client side interceptor example for auth looks like:
func FakeAuthUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { newCtx := context.WithValue(ctx, "user_id", "john@example.com") return handler(newCtx, req) }
Unfortunately, it's not as easy for streaming RPCs. These have the `context.Context` embedded within the `grpc.ServerStream` object. To pass values through context, a wrapper (`WrappedServerStream`) is needed. For example:
func FakeAuthStreamingInterceptor(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { newStream := middleware.WrapServerStream(stream) newStream.WrappedContext = context.WithValue(ctx, "user_id", "john@example.com") return handler(srv, newStream) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type WrappedServerStream ¶
type WrappedServerStream struct { grpc.ServerStream // WrappedContext is the wrapper's own Context. You can assign it. WrappedContext context.Context }
WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context.
func WrapServerStream ¶
func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream
WrapServerStream returns a ServerStream that has the ability to overwrite context.
func (*WrappedServerStream) Context ¶
func (w *WrappedServerStream) Context() context.Context
Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()
Directories ¶
Path | Synopsis |
---|---|
interceptor is an internal package used by higher level middlewares.
|
interceptor is an internal package used by higher level middlewares. |
auth
Package auth is a middleware that authenticates incoming gRPC requests.
|
Package auth is a middleware that authenticates incoming gRPC requests. |
logging
Package logging is a "parent" package for gRPC logging middlewares.
|
Package logging is a "parent" package for gRPC logging middlewares. |
protovalidate
Package protovalidate is a request validator that uses https://github.com/bufbuild/protovalidate-go under the hood.
|
Package protovalidate is a request validator that uses https://github.com/bufbuild/protovalidate-go under the hood. |
ratelimit
Package ratelimit is a middleware that limits the rate of requests.
|
Package ratelimit is a middleware that limits the rate of requests. |
realip
Package realip is a middleware that extracts the real IP of requests based on header values.
|
Package realip is a middleware that extracts the real IP of requests based on header values. |
recovery
Package recovery is a middleware that recovers from panics and logs the panic message.
|
Package recovery is a middleware that recovers from panics and logs the panic message. |
retry
Package retry provides client-side request retry logic for gRPC.
|
Package retry provides client-side request retry logic for gRPC. |
selector
Package selector
|
Package selector |
timeout
Package timeout is a middleware that responds with a timeout error after the given duration.
|
Package timeout is a middleware that responds with a timeout error after the given duration. |
validator
Package validator
|
Package validator |
testing
|
|
util
|
|
backoffutils
Package backoffutils implements common backoff features.
|
Package backoffutils implements common backoff features. |