Trait ntex::Middleware
source · pub trait Middleware<S> {
type Service;
// Required method
fn create(&self, service: S) -> Self::Service;
}
Expand description
The Middleware
trait defines the interface of a service factory that wraps inner service
during construction.
Middleware wraps inner service and runs during inbound and/or outbound processing in the request/response lifecycle. It may modify request and/or response.
For example, timeout middleware:
pub struct Timeout<S> {
service: S,
timeout: Duration,
}
impl<S, R> Service<R> for Timeout<S>
where
S: Service<R>,
{
type Response = S::Response;
type Error = TimeoutError<S::Error>;
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx).map_err(TimeoutError::Service)
}
async fn call(&self, req: S::Request) -> Result<Self::Response, Self::Error> {
match select(sleep(self.timeout), ctx.call(&self.service, req)).await {
Either::Left(_) => Err(TimeoutError::Timeout),
Either::Right(res) => res.map_err(TimeoutError::Service),
}
}
}
Timeout service in above example is decoupled from underlying service implementation and could be applied to any service.
The Middleware
trait defines the interface of a middleware factory, defining how to
construct a middleware Service. A Service that is constructed by the factory takes
the Service that follows it during execution as a parameter, assuming
ownership of the next Service.
Factory for Timeout
middleware from the above example could look like this:
pub struct TimeoutMiddleware {
timeout: Duration,
}
impl<S> Middleware<S> for TimeoutMiddleware<E>
{
type Service = Timeout<S>;
fn create(&self, service: S) -> Self::Service {
ok(Timeout {
service,
timeout: self.timeout,
})
}
}