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> Service for Timeout<S>
where
    S: Service,
{
    type Request = S::Request;
    type Response = S::Response;
    type Error = TimeoutError<S::Error>;
    type Future = TimeoutResponse<S>;

    fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        ready!(self.service.poll_ready(cx)).map_err(TimeoutError::Service)
    }

    fn call(&self, req: S::Request) -> Self::Future {
        TimeoutServiceResponse {
            fut: self.service.call(req),
            sleep: Delay::new(clock::now() + self.timeout),
        }
    }
}

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>
where
    S: Service,
{
    type Service = Timeout<S>;

    fn create(&self, service: S) -> Self::Service {
        ok(Timeout {
            service,
            timeout: self.timeout,
        })
    }
}

Required Associated Types§

source

type Service

The middleware Service value created by this factory

Required Methods§

source

fn create(&self, service: S) -> Self::Service

Creates and returns a new middleware Service

Implementations on Foreign Types§

source§

impl<T, S> Middleware<S> for Rc<T>where T: Middleware<S>,

§

type Service = <T as Middleware<S>>::Service

source§

fn create(&self, service: S) -> <T as Middleware<S>>::Service

Implementors§

source§

impl<R, S, E> Middleware<S> for Buffer<R, E>where S: Service<R, Error = E>,

§

type Service = BufferService<R, S, E>

source§

impl<S> Middleware<S> for Identity

§

type Service = S

source§

impl<S> Middleware<S> for InFlight

source§

impl<S> Middleware<S> for Timeout<()>

source§

impl<S> Middleware<S> for Compress

§

type Service = CompressMiddleware<S>

source§

impl<S> Middleware<S> for DefaultHeaders

§

type Service = DefaultHeadersMiddleware<S>

source§

impl<S> Middleware<S> for Logger

§

type Service = LoggerMiddleware<S>

source§

impl<S, Inner, Outer> Middleware<S> for Stack<Inner, Outer>where Inner: Middleware<S>, Outer: Middleware<<Inner as Middleware<S>>::Service>,

§

type Service = <Outer as Middleware<<Inner as Middleware<S>>::Service>>::Service