pub trait Service<Request> {
type Response;
type Error;
type Future: Future;
fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::Error>>;
fn call(&mut self, req: Request) -> Self::Future;
}Expand description
An asynchronous function from a Request to a Response.
The Service trait is a simplified interface making it easy to write
network applications in a modular and reusable way, decoupled from the
underlying protocol. It is one of Tower’s fundamental abstractions.
Functional
A Service is a function of a Request. It immediately returns a
Future representing the eventual completion of processing the
request. The actual request processing may happen at any time in the
future, on any thread or executor. The processing may depend on calling
other services. At some point in the future, the processing will complete,
and the Future will resolve to a response or error.
At a high level, the Service::call function represents an RPC request. The
Service value can be a server or a client.
Server
An RPC server implements the Service trait. Requests received by the
server over the network are deserialized and then passed as an argument to the
server value. The returned response is sent back over the network.
As an example, here is how an HTTP request is processed by a server:
use http::{Request, Response, StatusCode};
struct HelloWorld;
impl Service<Request<Vec<u8>>> for HelloWorld {
type Response = Response<Vec<u8>>;
type Error = http::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<Vec<u8>>) -> Self::Future {
// create the body
let body: Vec<u8> = "hello, world!\n"
.as_bytes()
.to_owned();
// Create the HTTP response
let resp = Response::builder()
.status(StatusCode::OK)
.body(body)
.expect("Unable to create `http::Response`");
// create a response in a future.
let fut = async {
Ok(resp)
};
// Return the response as an immediate future
Box::pin(fut)
}
}Client
A client consumes a service by using a Service value. The client may
issue requests by invoking call and passing the request as an argument.
It then receives the response by waiting for the returned future.
As an example, here is how a Redis request would be issued:
let client = redis::Client::new()
.connect("127.0.0.1:6379".parse().unwrap())
.unwrap();
let resp = client.call(Cmd::set("foo", "this is the value of foo")).await?;
// Wait for the future to resolve
println!("Redis response: {:?}", resp);Middleware / Layer
More often than not, all the pieces needed for writing robust, scalable network applications are the same no matter the underlying protocol. By unifying the API for both clients and servers in a protocol agnostic way, it is possible to write middleware that provide these pieces in a reusable way.
Take timeouts as an example:
use tower_service::Service;
use tower_layer::Layer;
use futures::FutureExt;
use std::future::Future;
use std::task::{Context, Poll};
use std::time::Duration;
use std::pin::Pin;
use std::fmt;
use std::error::Error;
// Our timeout service, which wraps another service and
// adds a timeout to its response future.
pub struct Timeout<T> {
inner: T,
timeout: Duration,
}
impl<T> Timeout<T> {
pub fn new(inner: T, timeout: Duration) -> Timeout<T> {
Timeout {
inner,
timeout
}
}
}
// The error returned if processing a request timed out
#[derive(Debug)]
pub struct Expired;
impl fmt::Display for Expired {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "expired")
}
}
impl Error for Expired {}
// We can implement `Service` for `Timeout<T>` if `T` is a `Service`
impl<T, Request> Service<Request> for Timeout<T>
where
T: Service<Request>,
T::Future: 'static,
T::Error: Into<Box<dyn Error + Send + Sync>> + 'static,
T::Response: 'static,
{
// `Timeout` doesn't modify the response type, so we use `T`'s response type
type Response = T::Response;
// Errors may be either `Expired` if the timeout expired, or the inner service's
// `Error` type. Therefore, we return a boxed `dyn Error + Send + Sync` trait object to erase
// the error's type.
type Error = Box<dyn Error + Send + Sync>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
// Our timeout service is ready if the inner service is ready.
// This is how backpressure can be propagated through a tree of nested services.
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, req: Request) -> Self::Future {
// Create a future that completes after `self.timeout`
let timeout = tokio::time::sleep(self.timeout);
// Call the inner service and get a future that resolves to the response
let fut = self.inner.call(req);
// Wrap those two futures in another future that completes when either one completes
//
// If the inner service is too slow the `sleep` future will complete first
// And an error will be returned and `fut` will be dropped and not polled again
//
// We have to box the errors so the types match
let f = async move {
tokio::select! {
res = fut => {
res.map_err(|err| err.into())
},
_ = timeout => {
Err(Box::new(Expired) as Box<dyn Error + Send + Sync>)
},
}
};
Box::pin(f)
}
}
// A layer for wrapping services in `Timeout`
pub struct TimeoutLayer(Duration);
impl TimeoutLayer {
pub fn new(delay: Duration) -> Self {
TimeoutLayer(delay)
}
}
impl<S> Layer<S> for TimeoutLayer {
type Service = Timeout<S>;
fn layer(&self, service: S) -> Timeout<S> {
Timeout::new(service, self.0)
}
}The above timeout implementation is decoupled from the underlying protocol and is also decoupled from client or server concerns. In other words, the same timeout middleware could be used in either a client or a server.
Backpressure
Calling a Service which is at capacity (i.e., it is temporarily unable to process a
request) should result in an error. The caller is responsible for ensuring
that the service is ready to receive the request before calling it.
Service provides a mechanism by which the caller is able to coordinate
readiness. Service::poll_ready returns Ready if the service expects that
it is able to process a request.
Associated Types
Required methods
Returns Poll::Ready(Ok(())) when the service is able to process requests.
If the service is at capacity, then Poll::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. Generally, this can be done with
a simple futures::future::poll_fn call.
If Poll::Ready(Err(_)) is returned, the service is no longer able to service requests
and the caller should discard the service instance.
Once poll_ready returns Poll::Ready(Ok(())), a request may be dispatched to the
service using call. Until a request is dispatched, repeated calls to
poll_ready must return either Poll::Ready(Ok(())) or Poll::Ready(Err(_)).
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.
Before dispatching a request, poll_ready must be called and return
Poll::Ready(Ok(())).
Panics
Implementations are permitted to panic if call is invoked without
obtaining Poll::Ready(Ok(())) from poll_ready.
Implementations on Foreign Types
sourceimpl<'a, S, Request> Service<Request> for &'a mut S where
S: 'a + Service<Request>,
impl<'a, S, Request> Service<Request> for &'a mut S where
S: 'a + Service<Request>,
type Response = <S as Service<Request>>::Response
type Error = <S as Service<Request>>::Error
type Future = <S as Service<Request>>::Future
pub fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), <S as Service<Request>>::Error>>
pub fn call(&mut self, request: Request) -> <S as Service<Request>>::Future
sourceimpl<S, Request> Service<Request> for Box<S, Global> where
S: Service<Request> + ?Sized,
impl<S, Request> Service<Request> for Box<S, Global> where
S: Service<Request> + ?Sized,
type Response = <S as Service<Request>>::Response
type Error = <S as Service<Request>>::Error
type Future = <S as Service<Request>>::Future
pub fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), <S as Service<Request>>::Error>>
pub fn call(&mut self, request: Request) -> <S as Service<Request>>::Future
Implementors
sourceimpl<A, B, Request> Service<Request> for Either<A, B> where
A: Service<Request>,
A::Error: Into<BoxError>,
B: Service<Request, Response = A::Response>,
B::Error: Into<BoxError>,
This is supported on crate feature util only.
impl<A, B, Request> Service<Request> for Either<A, B> where
A: Service<Request>,
A::Error: Into<BoxError>,
B: Service<Request, Response = A::Response>,
B::Error: Into<BoxError>,
util only.sourceimpl<D, Req> Service<Req> for Balance<D, Req> where
D: Discover + Unpin,
D::Key: Hash + Clone,
D::Error: Into<BoxError>,
D::Service: Service<Req> + Load,
<D::Service as Load>::Metric: Debug,
<D::Service as Service<Req>>::Error: Into<BoxError>,
This is supported on crate feature balance only.
impl<D, Req> Service<Req> for Balance<D, Req> where
D: Discover + Unpin,
D::Key: Hash + Clone,
D::Error: Into<BoxError>,
D::Service: Service<Req> + Load,
<D::Service as Load>::Metric: Debug,
<D::Service as Service<Req>>::Error: Into<BoxError>,
balance only.sourceimpl<F, S, R, E> Service<R> for FutureService<F, S> where
F: Future<Output = Result<S, E>> + Unpin,
S: Service<R, Error = E>,
This is supported on crate feature util only.
impl<F, S, R, E> Service<R> for FutureService<F, S> where
F: Future<Output = Result<S, E>> + Unpin,
S: Service<R, Error = E>,
util only.sourceimpl<M, S, Target, Request> Service<Target> for AsService<'_, M, Request> where
M: Service<Target, Response = S>,
S: Service<Request>,
This is supported on crate feature make only.
impl<M, S, Target, Request> Service<Target> for AsService<'_, M, Request> where
M: Service<Target, Response = S>,
S: Service<Request>,
make only.sourceimpl<M, S, Target, Request> Service<Target> for IntoService<M, Request> where
M: Service<Target, Response = S>,
S: Service<Request>,
This is supported on crate feature make only.
impl<M, S, Target, Request> Service<Target> for IntoService<M, Request> where
M: Service<Target, Response = S>,
S: Service<Request>,
make only.sourceimpl<M, Target, S, Request> Service<Request> for Reconnect<M, Target> where
M: Service<Target, Response = S>,
S: Service<Request>,
M::Future: Unpin,
BoxError: From<M::Error> + From<S::Error>,
Target: Clone,
This is supported on crate feature reconnect only.
impl<M, Target, S, Request> Service<Request> for Reconnect<M, Target> where
M: Service<Target, Response = S>,
S: Service<Request>,
M::Future: Unpin,
BoxError: From<M::Error> + From<S::Error>,
Target: Clone,
reconnect only.sourceimpl<MS, Target, Req> Service<Req> for Pool<MS, Target, Req> where
MS: MakeService<Target, Req>,
MS::Service: Load,
<MS::Service as Load>::Metric: Debug,
MS::MakeError: Into<BoxError>,
MS::Error: Into<BoxError>,
Target: Clone,
This is supported on crate feature balance only.
impl<MS, Target, Req> Service<Req> for Pool<MS, Target, Req> where
MS: MakeService<Target, Req>,
MS::Service: Load,
<MS::Service as Load>::Metric: Debug,
MS::MakeError: Into<BoxError>,
MS::Error: Into<BoxError>,
Target: Clone,
balance only.sourceimpl<P, S, Request> Service<Request> for Retry<P, S> where
P: Policy<Request, S::Response, S::Error> + Clone,
S: Service<Request> + Clone,
This is supported on crate feature retry only.
impl<P, S, Request> Service<Request> for Retry<P, S> where
P: Policy<Request, S::Response, S::Error> + Clone,
S: Service<Request> + Clone,
retry only.sourceimpl<R, S, F, T, E, Fut> Service<R> for MapFuture<S, F> where
S: Service<R>,
F: FnMut(S::Future) -> Fut,
E: From<S::Error>,
Fut: Future<Output = Result<T, E>>,
This is supported on crate feature util only.
impl<R, S, F, T, E, Fut> Service<R> for MapFuture<S, F> where
S: Service<R>,
F: FnMut(S::Future) -> Fut,
E: From<S::Error>,
Fut: Future<Output = Result<T, E>>,
util only.sourceimpl<S, C, Request> Service<Request> for PeakEwma<S, C> where
S: Service<Request>,
C: TrackCompletion<Handle, S::Response>,
This is supported on crate feature load only.
impl<S, C, Request> Service<Request> for PeakEwma<S, C> where
S: Service<Request>,
C: TrackCompletion<Handle, S::Response>,
load only.sourceimpl<S, C, Request> Service<Request> for PendingRequests<S, C> where
S: Service<Request>,
C: TrackCompletion<Handle, S::Response>,
This is supported on crate feature load only.
impl<S, C, Request> Service<Request> for PendingRequests<S, C> where
S: Service<Request>,
C: TrackCompletion<Handle, S::Response>,
load only.sourceimpl<S, F, R1, R2> Service<R1> for MapRequest<S, F> where
S: Service<R2>,
F: FnMut(R1) -> R2,
This is supported on crate feature util only.
impl<S, F, R1, R2> Service<R1> for MapRequest<S, F> where
S: Service<R2>,
F: FnMut(R1) -> R2,
util only.sourceimpl<S, F, Request, Error> Service<Request> for MapErr<S, F> where
S: Service<Request>,
F: FnOnce(S::Error) -> Error + Clone,
This is supported on crate feature util only.
impl<S, F, Request, Error> Service<Request> for MapErr<S, F> where
S: Service<Request>,
F: FnOnce(S::Error) -> Error + Clone,
util only.sourceimpl<S, F, Request, Fut> Service<Request> for AndThen<S, F> where
S: Service<Request>,
S::Error: Into<Fut::Error>,
F: FnOnce(S::Response) -> Fut + Clone,
Fut: TryFuture,
This is supported on crate feature util only.
impl<S, F, Request, Fut> Service<Request> for AndThen<S, F> where
S: Service<Request>,
S::Error: Into<Fut::Error>,
F: FnOnce(S::Response) -> Fut + Clone,
Fut: TryFuture,
util only.sourceimpl<S, F, Request, Response> Service<Request> for MapResponse<S, F> where
S: Service<Request>,
F: FnOnce(S::Response) -> Response + Clone,
This is supported on crate feature util only.
impl<S, F, Request, Response> Service<Request> for MapResponse<S, F> where
S: Service<Request>,
F: FnOnce(S::Response) -> Response + Clone,
util only.sourceimpl<S, F, Request, Response, Error> Service<Request> for MapResult<S, F> where
S: Service<Request>,
Error: From<S::Error>,
F: FnOnce(Result<S::Response, S::Error>) -> Result<Response, Error> + Clone,
This is supported on crate feature util only.
impl<S, F, Request, Response, Error> Service<Request> for MapResult<S, F> where
S: Service<Request>,
Error: From<S::Error>,
F: FnOnce(Result<S::Response, S::Error>) -> Result<Response, Error> + Clone,
util only.sourceimpl<S, F, Request, Response, Error, Fut> Service<Request> for Then<S, F> where
S: Service<Request>,
S::Error: Into<Error>,
F: FnOnce(Result<S::Response, S::Error>) -> Fut + Clone,
Fut: Future<Output = Result<Response, Error>>,
This is supported on crate feature util only.
impl<S, F, Request, Response, Error, Fut> Service<Request> for Then<S, F> where
S: Service<Request>,
S::Error: Into<Error>,
F: FnOnce(Result<S::Response, S::Error>) -> Fut + Clone,
Fut: Future<Output = Result<Response, Error>>,
util only.sourceimpl<S, M, Request> Service<Request> for Constant<S, M> where
S: Service<Request>,
M: Copy,
This is supported on crate feature load only.
impl<S, M, Request> Service<Request> for Constant<S, M> where
S: Service<Request>,
M: Copy,
load only.sourceimpl<S, P, Request> Service<Request> for Hedge<S, P> where
S: Service<Request> + Clone,
S::Error: Into<BoxError>,
P: Policy<Request> + Clone,
This is supported on crate feature hedge only.
impl<S, P, Request> Service<Request> for Hedge<S, P> where
S: Service<Request> + Clone,
S::Error: Into<BoxError>,
P: Policy<Request> + Clone,
hedge only.sourceimpl<S, Req> Service<Req> for LoadShed<S> where
S: Service<Req>,
S::Error: Into<BoxError>,
This is supported on crate feature load-shed only.
impl<S, Req> Service<Req> for LoadShed<S> where
S: Service<Req>,
S::Error: Into<BoxError>,
load-shed only.sourceimpl<S, Req> Service<Req> for SpawnReady<S> where
Req: 'static,
S: Service<Req> + Send + 'static,
S::Error: Into<BoxError>,
This is supported on crate feature spawn-ready only.
impl<S, Req> Service<Req> for SpawnReady<S> where
Req: 'static,
S: Service<Req> + Send + 'static,
S::Error: Into<BoxError>,
spawn-ready only.sourceimpl<S, Req, F> Service<Req> for Steer<S, F, Req> where
S: Service<Req>,
F: Picker<S, Req>,
This is supported on crate feature steer only.
impl<S, Req, F> Service<Req> for Steer<S, F, Req> where
S: Service<Req>,
F: Picker<S, Req>,
steer only.sourceimpl<S, Request> Service<Request> for ConcurrencyLimit<S> where
S: Service<Request>,
This is supported on crate feature limit only.
impl<S, Request> Service<Request> for ConcurrencyLimit<S> where
S: Service<Request>,
limit only.sourceimpl<S, Request> Service<Request> for RateLimit<S> where
S: Service<Request>,
This is supported on crate feature limit only.
impl<S, Request> Service<Request> for RateLimit<S> where
S: Service<Request>,
limit only.sourceimpl<S, Request> Service<Request> for Timeout<S> where
S: Service<Request>,
S::Error: Into<BoxError>,
This is supported on crate feature timeout only.
impl<S, Request> Service<Request> for Timeout<S> where
S: Service<Request>,
S::Error: Into<BoxError>,
timeout only.sourceimpl<S, T> Service<T> for Shared<S> where
S: Clone,
This is supported on crate feature make only.
impl<S, T> Service<T> for Shared<S> where
S: Clone,
make only.sourceimpl<S, Target> Service<Target> for MakeSpawnReady<S> where
S: Service<Target>,
This is supported on crate feature spawn-ready only.
impl<S, Target> Service<Target> for MakeSpawnReady<S> where
S: Service<Target>,
spawn-ready only.sourceimpl<S, Target, Req> Service<Target> for MakeBalance<S, Req> where
S: Service<Target>,
S::Response: Discover,
<S::Response as Discover>::Key: Hash,
<S::Response as Discover>::Service: Service<Req>,
<<S::Response as Discover>::Service as Service<Req>>::Error: Into<BoxError>,
This is supported on crate feature balance only.
impl<S, Target, Req> Service<Target> for MakeBalance<S, Req> where
S: Service<Target>,
S::Response: Discover,
<S::Response as Discover>::Key: Hash,
<S::Response as Discover>::Service: Service<Req>,
<<S::Response as Discover>::Service as Service<Req>>::Error: Into<BoxError>,
balance only.sourceimpl<T, F, Request, R, E> Service<Request> for ServiceFn<T> where
T: FnMut(Request) -> F,
F: Future<Output = Result<R, E>>,
This is supported on crate feature util only.
impl<T, F, Request, R, E> Service<Request> for ServiceFn<T> where
T: FnMut(Request) -> F,
F: Future<Output = Result<R, E>>,
util only.sourceimpl<T, Request> Service<Request> for Buffer<T, Request> where
T: Service<Request>,
T::Error: Into<BoxError>,
This is supported on crate feature buffer only.
impl<T, Request> Service<Request> for Buffer<T, Request> where
T: Service<Request>,
T::Error: Into<BoxError>,
buffer only.sourceimpl<T, Request> Service<Request> for Optional<T> where
T: Service<Request>,
T::Error: Into<BoxError>,
This is supported on crate feature util only.
impl<T, Request> Service<Request> for Optional<T> where
T: Service<Request>,
T::Error: Into<BoxError>,
util only.sourceimpl<T, U, E> Service<T> for BoxCloneService<T, U, E>
This is supported on crate feature util only.
impl<T, U, E> Service<T> for BoxCloneService<T, U, E>
util only.sourceimpl<T, U, E> Service<T> for BoxService<T, U, E>
This is supported on crate feature util only.
impl<T, U, E> Service<T> for BoxService<T, U, E>
util only.sourceimpl<T, U, E> Service<T> for UnsyncBoxService<T, U, E>
This is supported on crate feature util only.
impl<T, U, E> Service<T> for UnsyncBoxService<T, U, E>
util only.sourceimpl<T, U, Request> Service<Request> for AsyncFilter<T, U> where
U: AsyncPredicate<Request>,
T: Service<U::Request> + Clone,
T::Error: Into<BoxError>,
This is supported on crate feature filter only.
impl<T, U, Request> Service<Request> for AsyncFilter<T, U> where
U: AsyncPredicate<Request>,
T: Service<U::Request> + Clone,
T::Error: Into<BoxError>,
filter only.