11//! gRPC interceptors which are a kind of middleware.
22//!
3- //! See [`interceptor_fn `] for more details.
3+ //! See [`Interceptor `] for more details.
44
55use crate :: { request:: SanitizeHeaders , Status } ;
66use pin_project:: pin_project;
@@ -13,12 +13,15 @@ use std::{
1313use tower_layer:: Layer ;
1414use tower_service:: Service ;
1515
16- /// Create a new interceptor from a function .
16+ /// A gRPC incerceptor .
1717///
1818/// gRPC interceptors are similar to middleware but have less flexibility. An interceptor allows
1919/// you to do two main things, one is to add/remove/check items in the `MetadataMap` of each
2020/// request. Two, cancel a request with a `Status`.
2121///
22+ /// Any function that satisfies the bound `FnMut(Request<()>) -> Result<Request<()>, Status>` can be
23+ /// used as an `Interceptor`.
24+ ///
2225/// An interceptor can be used on both the server and client side through the `tonic-build` crate's
2326/// generated structs.
2427///
@@ -35,24 +38,56 @@ use tower_service::Service;
3538/// [tower]: https://crates.io/crates/tower
3639/// [example]: https://github.com/hyperium/tonic/tree/master/examples/src/interceptor
3740/// [tower-example]: https://github.com/hyperium/tonic/tree/master/examples/src/tower
38- pub fn interceptor_fn < F > ( f : F ) -> InterceptorFn < F >
41+ pub trait Interceptor {
42+ /// Intercept a request before it is sent, optionally cancelling it.
43+ fn call ( & mut self , request : crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > ;
44+ }
45+
46+ impl < F > Interceptor for F
3947where
4048 F : FnMut ( crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > ,
4149{
42- InterceptorFn { f }
50+ fn call ( & mut self , request : crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > {
51+ self ( request)
52+ }
53+ }
54+
55+ /// Create a new interceptor layer.
56+ ///
57+ /// See [`Interceptor`] for more details.
58+ pub fn interceptor < F > ( f : F ) -> InterceptorLayer < F >
59+ where
60+ F : Interceptor ,
61+ {
62+ InterceptorLayer { f }
4363}
4464
45- /// An interceptor created from a function.
65+ #[ deprecated(
66+ since = "0.5.1" ,
67+ note = "Please use the `interceptor` function instead"
68+ ) ]
69+ /// Create a new interceptor layer.
4670///
47- /// See [`interceptor_fn`] for more details.
71+ /// See [`Interceptor`] for more details.
72+ pub fn interceptor_fn < F > ( f : F ) -> InterceptorLayer < F >
73+ where
74+ F : Interceptor ,
75+ {
76+ interceptor ( f)
77+ }
78+
79+ /// A gRPC interceptor that can be used as a [`Layer`],
80+ /// created by calling [`interceptor`].
81+ ///
82+ /// See [`Interceptor`] for more details.
4883#[ derive( Debug , Clone , Copy ) ]
49- pub struct InterceptorFn < F > {
84+ pub struct InterceptorLayer < F > {
5085 f : F ,
5186}
5287
53- impl < S , F > Layer < S > for InterceptorFn < F >
88+ impl < S , F > Layer < S > for InterceptorLayer < F >
5489where
55- F : FnMut ( crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > + Clone ,
90+ F : Interceptor + Clone ,
5691{
5792 type Service = InterceptedService < S , F > ;
5893
6196 }
6297}
6398
99+ #[ deprecated(
100+ since = "0.5.1" ,
101+ note = "Please use the `InterceptorLayer` type instead"
102+ ) ]
103+ /// A gRPC interceptor that can be used as a [`Layer`],
104+ /// created by calling [`interceptor`].
105+ ///
106+ /// See [`Interceptor`] for more details.
107+ pub type InterceptorFn < F > = InterceptorLayer < F > ;
108+
64109/// A service wrapped in an interceptor middleware.
65110///
66- /// See [`interceptor_fn `] for more details.
111+ /// See [`Interceptor `] for more details.
67112#[ derive( Clone , Copy ) ]
68113pub struct InterceptedService < S , F > {
69114 inner : S ,
@@ -75,7 +120,7 @@ impl<S, F> InterceptedService<S, F> {
75120 /// function `F`.
76121 pub fn new ( service : S , f : F ) -> Self
77122 where
78- F : FnMut ( crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > ,
123+ F : Interceptor ,
79124 {
80125 Self { inner : service, f }
81126 }
95140
96141impl < S , F , ReqBody , ResBody > Service < http:: Request < ReqBody > > for InterceptedService < S , F >
97142where
98- F : FnMut ( crate :: Request < ( ) > ) -> Result < crate :: Request < ( ) > , Status > ,
143+ F : Interceptor ,
99144 S : Service < http:: Request < ReqBody > , Response = http:: Response < ResBody > > ,
100145 S :: Error : Into < crate :: Error > ,
101146{
@@ -113,7 +158,10 @@ where
113158 let req = crate :: Request :: from_http ( req) ;
114159 let ( metadata, extensions, msg) = req. into_parts ( ) ;
115160
116- match ( self . f ) ( crate :: Request :: from_parts ( metadata, extensions, ( ) ) ) {
161+ match self
162+ . f
163+ . call ( crate :: Request :: from_parts ( metadata, extensions, ( ) ) )
164+ {
117165 Ok ( req) => {
118166 let ( metadata, extensions, _) = req. into_parts ( ) ;
119167 let req = crate :: Request :: from_parts ( metadata, extensions, msg) ;
0 commit comments