      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306

    #![doc(html_root_url = "https://docs.rs/tower-service/0.3.1")]
    #![warn(
        missing_debug_implementations,
        missing_docs,
        rust_2018_idioms,
        unreachable_pub
    )]
    #![deny(broken_intra_doc_links)]

    //! Definition of the core `Service` trait to Tower
    //!
    //! The [`Service`] trait provides the necessary abstractions for defining
    //! request / response clients and servers. It is simple but powerful and is
    //! used as the foundation for the rest of Tower.

    use std::future::Future;
    use std::task::{Context, Poll};

    /// 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:
    ///
    /// ```rust
    /// # use std::pin::Pin;
    /// # use std::task::{Poll, Context};
    /// # use std::future::Future;
    /// # use tower_service::Service;
    /// 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:
    ///
    /// ```rust,ignore
    /// 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:
    ///
    /// ```rust
    /// 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.
    pub trait Service<Request> {
        /// Responses given by the service.
        type Response;

        /// Errors produced by the service.
        type Error;

        /// The future response value.
        type Future: Future<Output = Result<Self::Response, Self::Error>>;

        /// 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(_))`.
        fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;

        /// 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`.
        fn call(&mut self, req: Request) -> Self::Future;
    }

    impl<'a, S, Request> Service<Request> for &'a mut S
    where
        S: Service<Request> + 'a,
    {
        type Response = S::Response;
        type Error = S::Error;
        type Future = S::Future;

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

        fn call(&mut self, request: Request) -> S::Future {
            (**self).call(request)
        }
    }

    impl<S, Request> Service<Request> for Box<S>
    where
        S: Service<Request> + ?Sized,
    {
        type Response = S::Response;
        type Error = S::Error;
        type Future = S::Future;

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

        fn call(&mut self, request: Request) -> S::Future {
            (**self).call(request)
        }
    }