Trait tonic::service::Interceptor

source ·
pub trait Interceptor {
    // Required method
    fn call(&mut self, request: Request<()>) -> Result<Request<()>, Status>;
Expand description

A gRPC interceptor.

gRPC interceptors are similar to middleware but have less flexibility. An interceptor allows you to do two main things, one is to add/remove/check items in the MetadataMap of each request. Two, cancel a request with a Status.

Any function that satisfies the bound FnMut(Request<()>) -> Result<Request<()>, Status> can be used as an Interceptor.

An interceptor can be used on both the server and client side through the tonic-build crate’s generated structs.

See the interceptor example for more details.

If you need more powerful middleware, tower is the recommended approach. You can find examples of how to use tower with tonic here.

Additionally, interceptors is not the recommended way to add logging to your service. For that a tower middleware is more appropriate since it can also act on the response. For example tower-http’s Trace middleware supports gRPC out of the box.

Required Methods§


fn call(&mut self, request: Request<()>) -> Result<Request<()>, Status>

Intercept a request before it is sent, optionally cancelling it.



impl<F> Interceptor for F
where F: FnMut(Request<()>) -> Result<Request<()>, Status>,