tonic-middleware
Table of Contents
- Introduction
- Tonic versions compatability
- Usage
- Define request interceptor and middleware
- Apply request interceptor to individual service
- Apply request interceptor to all services using layer
- Apply middleware to individual services
- Apply middleware to all services through layer
- Combine interceptor and middleware for individual services
- Apply interceptor and middleware to all services through layer
- Full example or check integration tests
- Motivation
Introduction
tonic-middleware is a Rust library that extends tonic-based gRPC services,
enabling asynchronous inspection and modification and potentially rejecting of incoming requests.
It also enables the addition of custom logic through middleware, both before and after the actual service call.
The library provides two key tools:
-
Request Interceptor
The
RequestInterceptortrait is designed to enable the interception and processing of incoming requests within your service pipeline. This trait is particularly useful for performing operations such as authentication, enriching requests with additional metadata, or rejecting requests based on certain criteria before they reach the service logic. -
Middleware
If your requirements extend beyond request interception, and you need to interact with both the request and response or to perform actions after the service call has been made, you should consider implementing
Middleware.
Both interceptors and middlewares can be applied to individual service, or to all services through Tonic's layer.
Tonic versions compatability
| tonic version | tonic-middleware version |
|---|---|
| 0.11 | 0.14 |
| 0.12 | 0.2.0 |
Usage
Add to Cargo.toml
tonic-middleware = "0.2.0"
See full example or check integration tests
Define our request interceptor and middleware
To create request interceptor, we need to implement RequestInterceptor trait from the library.
Simple request interceptor that uses some custom AuthService injected in to perform authentication.
We need to implement RequestInterceptor for our custom (AuthInterceptor) intercept.
To create middleware, we need to implement 'Middleware' trait from the library.
Metrics middleware that measures request time and output to stdout.
We need to implement Middleware for our custom (MetricsMiddleware) middleware.
;
Apply request interceptor to individual service
async
Apply request interceptor to all services using layer
async
Apply middleware to individual services
async
Apply middleware to all services through layer
async
Combine interceptor and middleware for individual services
async
Apply interceptor and middleware to all services through layer
async
Motivation
Tonic provides a solid foundation for developing gRPC services in Rust, and while it offers a range of features, extending it with asynchronous interceptors and middleware requires a bit more effort. That's where tonic-middleware comes in,
this library simplifies adding custom asynchronous processing to the tonic service stack.