[−][src]Trait actix_service::Service
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 where each parameter is described as an
associated type on the trait. Services can also have mutable state that influence computation.
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 for MyService { type Request = u8; type Response = u64; type Error = MyError; type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... } fn call(&mut self, req: Self::Request) -> 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>;
Associated Types
type Response
[src]
Responses given by the service.
type Error
[src]
Errors produced by the service when polling readiness or executing call.
type Future: Future<Output = Result<Self::Response, Self::Error>>
[src]
The future response value.
Required methods
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut 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
.poll_ready()
might be called on different task from actual service call.- In case of chained services,
.poll_ready()
get called for all services at once.
pub fn call(&mut self, req: Req) -> Self::Future
[src]
Process the request and return the response asynchronously.
This function is expected to be callable off task. As such,
implementations 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.
Calling call
without calling poll_ready
is permitted. The
implementation must be resilient to this fact.
Implementations on Foreign Types
impl<S: ?Sized, Req> Service<Req> for Box<S> where
S: Service<Req>,
[src]
S: Service<Req>,
type Response = S::Response
type Error = S::Error
type Future = S::Future
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), S::Error>>
[src]
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), S::Error>>
pub fn call(&mut self, request: Req) -> S::Future
[src]
impl<S, Req> Service<Req> for RefCell<S> where
S: Service<Req>,
[src]
S: Service<Req>,
type Response = S::Response
type Error = S::Error
type Future = S::Future
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, request: Req) -> S::Future
[src]
impl<S, Req> Service<Req> for Rc<RefCell<S>> where
S: Service<Req>,
[src]
S: Service<Req>,
type Response = S::Response
type Error = S::Error
type Future = S::Future
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, request: Req) -> S::Future
[src]
Implementors
impl<'a, S, Req> Service<Req> for &'a mut S where
S: Service<Req> + 'a,
[src]
S: Service<Req> + 'a,
type Response = S::Response
type Error = S::Error
type Future = S::Future
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, request: Req) -> S::Future
[src]
impl<A, F, Req, Res> Service<Req> for Map<A, F, Req, Res> where
A: Service<Req>,
F: FnMut(A::Response) -> Res + Clone,
[src]
A: Service<Req>,
F: FnMut(A::Response) -> Res + Clone,
type Response = Res
type Error = A::Error
type Future = MapFuture<A, F, Req, Res>
pub fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, req: Req) -> Self::Future
[src]
impl<A, Req, F, E> Service<Req> for MapErr<A, Req, F, E> where
A: Service<Req>,
F: Fn(A::Error) -> E + Clone,
[src]
A: Service<Req>,
F: Fn(A::Error) -> E + Clone,
type Response = A::Response
type Error = E
type Future = MapErrFuture<A, Req, F, E>
pub fn poll_ready(
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
ctx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, req: Req) -> Self::Future
[src]
impl<F, Fut, Req, Res, Err> Service<Req> for FnService<F, Fut, Req, Res, Err> where
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
[src]
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
type Response = Res
type Error = Err
type Future = Fut
pub fn poll_ready(
&mut self,
_: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
_: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, req: Req) -> Self::Future
[src]
impl<F, Fut, Req, Res, Err> Service<Req> for FnServiceFactory<F, Fut, Req, Res, Err, ()> where
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
[src]
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
type Response = Res
type Error = Err
type Future = Fut
pub fn poll_ready(
&mut self,
_: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
_: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
pub fn call(&mut self, req: Req) -> Self::Future
[src]
impl<S, F, Fut, Req, In, Res, Err> Service<Req> for Apply<S, F, Req, In, Res, Err> where
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
[src]
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
type Response = Res
type Error = Err
type Future = Fut
pub fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>
[src]
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>