Trait actix_web::dev::Service

pub trait Service<Req> {
    type Response;
    type Error;
    type Future: Future<Output = Result<Self::Response, Self::Error>>;

    // Required methods
    fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
    fn call(&self, req: Req) -> Self::Future;
}
Expand description

An asynchronous operation from Request to a Response.

The Service trait models a request/response interaction, receiving requests and returning replies. You can think about a service as a function with one argument that returns some result asynchronously. Conceptually, the operation looks like this:

async fn(Request) -> Result<Response, Err>

The Service trait just generalizes this form. Requests are defined as a generic type parameter and responses and other details are defined as associated types on the trait impl. Notice that this design means that services can receive many request types and converge them to a single response type.

Services can also have mutable state that influence computation by using a Cell, RefCell or Mutex. Services intentionally do not take &mut self to reduce overhead in the common cases.

Service provides a symmetric and uniform API; the same abstractions can be used to represent both clients and servers. Services describe only transformation operations which encourage simple API surfaces. This leads to simpler design of each service, improves test-ability and makes composition easier.

struct MyService;

impl Service<u8> for MyService {
     type Response = u64;
     type Error = MyError;
     type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

     fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... }

     fn call(&self, req: u8) -> Self::Future { ... }
}

Sometimes it is not necessary to implement the Service trait. For example, the above service could be rewritten as a simple function and passed to fn_service.

async fn my_service(req: u8) -> Result<u64, MyError>;

let svc = fn_service(my_service)
svc.call(123)

Required Associated Types§

type Response

Responses given by the service.

type Error

Errors produced by the service when polling readiness or executing call.

type Future: Future<Output = Result<Self::Response, Self::Error>>

The future response value.

Required Methods§

fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Ready when the service is able to process requests.

If the service is at capacity, then Pending is returned and the task is notified when the service becomes ready again. This function is expected to be called while on a task.

This is a best effort implementation. False positives are permitted. It is permitted for the service to return Ready from a poll_ready call and the next invocation of call results in an error.

§Notes
  1. poll_ready might be called on a different task to call.
  2. In cases of chained services, .poll_ready() is called for all services at once.

fn call(&self, req: Req) -> Self::Future

Process the request and return the response asynchronously.

This function is expected to be callable off-task. As such, implementations of call should take care to not call poll_ready. If the service is at capacity and the request is unable to be handled, the returned Future should resolve to an error.

Invoking call without first invoking poll_ready is permitted. Implementations must be resilient to this fact.

Implementations on Foreign Types§

source§

impl Service<Request> for ExpectHandler

§

impl<'a, S, Req> Service<Req> for &'a S
where S: Service<Req> + 'a,

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Future = <S as Service<Req>>::Future

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <&'a S as Service<Req>>::Error>>

§

fn call(&self, request: Req) -> <S as Service<Req>>::Future

§

impl<'a, S, Req> Service<Req> for &'a mut S
where S: Service<Req> + 'a,

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Future = <S as Service<Req>>::Future

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <&'a mut S as Service<Req>>::Error>>

§

fn call(&self, request: Req) -> <S as Service<Req>>::Future

§

impl<IO> Service<IO> for AcceptorService
where IO: ActixStream,

§

type Response = TlsStream<IO>

§

type Error = TlsError<Error, Infallible>

§

type Future = AcceptFut<IO>

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <AcceptorService as Service<IO>>::Error>>

§

fn call(&self, io: IO) -> <AcceptorService as Service<IO>>::Future

§

impl<IO> Service<IO> for AcceptorService
where IO: ActixStream,

§

type Response = TlsStream<IO>

§

type Error = TlsError<Error, Infallible>

§

type Future = AcceptFut<IO>

§

fn poll_ready( &self, cx: &mut Context<'_> ) -> Poll<Result<(), <AcceptorService as Service<IO>>::Error>>

§

fn call(&self, req: IO) -> <AcceptorService as Service<IO>>::Future

§

impl<IO> Service<IO> for AcceptorService
where IO: ActixStream,

§

type Response = TlsStream<IO>

§

type Error = TlsError<Error, Infallible>

§

type Future = AcceptFut<IO>

§

fn poll_ready( &self, cx: &mut Context<'_> ) -> Poll<Result<(), <AcceptorService as Service<IO>>::Error>>

§

fn call(&self, req: IO) -> <AcceptorService as Service<IO>>::Future

§

impl<IO> Service<IO> for AcceptorService
where IO: ActixStream,

§

type Response = TlsStream<IO>

§

type Error = TlsError<Error, Infallible>

§

type Future = AcceptFut<IO>

§

fn poll_ready( &self, cx: &mut Context<'_> ) -> Poll<Result<(), <AcceptorService as Service<IO>>::Error>>

§

fn call(&self, req: IO) -> <AcceptorService as Service<IO>>::Future

§

impl<IO> Service<IO> for AcceptorService
where IO: ActixStream,

§

type Response = TlsStream<IO>

§

type Error = TlsError<Error, Infallible>

§

type Future = AcceptFut<IO>

§

fn poll_ready( &self, cx: &mut Context<'_> ) -> Poll<Result<(), <AcceptorService as Service<IO>>::Error>>

§

fn call(&self, req: IO) -> <AcceptorService as Service<IO>>::Future

§

impl<R> Service<ConnectInfo<R>> for ConnectorService
where R: Host,

§

type Response = Connection<R, TcpStream>

§

type Error = ConnectError

§

type Future = ConnectServiceResponse<R>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <ConnectorService as Service<ConnectInfo<R>>>::Error>>

§

fn call( &self, req: ConnectInfo<R> ) -> <ConnectorService as Service<ConnectInfo<R>>>::Future

§

impl<R> Service<ConnectInfo<R>> for ResolverService
where R: Host,

§

type Response = ConnectInfo<R>

§

type Error = ConnectError

§

type Future = ResolverFut<R>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <ResolverService as Service<ConnectInfo<R>>>::Error>>

§

fn call( &self, req: ConnectInfo<R> ) -> <ResolverService as Service<ConnectInfo<R>>>::Future

§

impl<R> Service<ConnectInfo<R>> for TcpConnectorService
where R: Host,

§

type Response = Connection<R, TcpStream>

§

type Error = ConnectError

§

type Future = TcpConnectorFut<R>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TcpConnectorService as Service<ConnectInfo<R>>>::Error>>

§

fn call( &self, req: ConnectInfo<R> ) -> <TcpConnectorService as Service<ConnectInfo<R>>>::Future

§

impl<R, IO> Service<Connection<R, IO>> for TlsConnectorService
where R: Host, IO: ActixStream,

§

type Response = Connection<R, TlsStream<IO>>

§

type Error = Error

§

type Future = ConnectFut<R, IO>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TlsConnectorService as Service<Connection<R, IO>>>::Error>>

§

fn call( &self, connection: Connection<R, IO> ) -> <TlsConnectorService as Service<Connection<R, IO>>>::Future

§

impl<R, IO> Service<Connection<R, IO>> for TlsConnectorService
where R: Host, IO: ActixStream,

§

type Response = Connection<R, TlsStream<IO>>

§

type Error = Error

§

type Future = ConnectFut<R, IO>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TlsConnectorService as Service<Connection<R, IO>>>::Error>>

§

fn call( &self, connection: Connection<R, IO> ) -> <TlsConnectorService as Service<Connection<R, IO>>>::Future

§

impl<R, IO> Service<Connection<R, IO>> for TlsConnectorService
where R: Host, IO: ActixStream,

§

type Response = Connection<R, SslStream<IO>>

§

type Error = Error

§

type Future = ConnectFut<R, IO>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TlsConnectorService as Service<Connection<R, IO>>>::Error>>

§

fn call( &self, stream: Connection<R, IO> ) -> <TlsConnectorService as Service<Connection<R, IO>>>::Future

§

impl<R, IO> Service<Connection<R, IO>> for TlsConnectorService
where R: Host, IO: ActixStream,

§

type Response = Connection<R, TlsStream<IO>>

§

type Error = Error

§

type Future = ConnectFut<R, IO>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TlsConnectorService as Service<Connection<R, IO>>>::Error>>

§

fn call( &self, connection: Connection<R, IO> ) -> <TlsConnectorService as Service<Connection<R, IO>>>::Future

§

impl<R, IO> Service<Connection<R, IO>> for TlsConnectorService
where R: Host, IO: ActixStream,

§

type Response = Connection<R, TlsStream<IO>>

§

type Error = Error

§

type Future = ConnectFut<R, IO>

§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <TlsConnectorService as Service<Connection<R, IO>>>::Error>>

§

fn call( &self, connection: Connection<R, IO> ) -> <TlsConnectorService as Service<Connection<R, IO>>>::Future

§

impl<S, Req> Service<Req> for Box<S>
where S: Service<Req> + ?Sized,

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Future = <S as Service<Req>>::Future

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <S as Service<Req>>::Error>>

§

fn call(&self, request: Req) -> <S as Service<Req>>::Future

§

impl<S, Req> Service<Req> for Rc<S>
where S: Service<Req> + ?Sized,

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Future = <S as Service<Req>>::Future

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <Rc<S> as Service<Req>>::Error>>

§

fn call(&self, request: Req) -> <S as Service<Req>>::Future

§

impl<S, Req> Service<Req> for RefCell<S>
where S: Service<Req>,

This impl is deprecated since v2 because the Service trait now receives shared reference.

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Future = <S as Service<Req>>::Future

§

fn poll_ready( &self, ctx: &mut Context<'_> ) -> Poll<Result<(), <RefCell<S> as Service<Req>>::Error>>

§

fn call(&self, request: Req) -> <S as Service<Req>>::Future

source§

impl<T> Service<(Request, Framed<T, Codec>)> for UpgradeHandler

§

type Response = ()

§

type Error = Error

§

type Future = Pin<Box<dyn Future<Output = Result<<UpgradeHandler as Service<(Request, Framed<T, Codec>)>>::Response, <UpgradeHandler as Service<(Request, Framed<T, Codec>)>>::Error>>>>

source§

fn poll_ready( &self, _: &mut Context<'_> ) -> Poll<Result<(), <UpgradeHandler as Service<(Request, Framed<T, Codec>)>>::Error>>

source§

fn call( &self, _: (Request, Framed<T, Codec>) ) -> <UpgradeHandler as Service<(Request, Framed<T, Codec>)>>::Future

Implementors§