2020-12-14 13:52:30 +05:30
|
|
|
//! See [`Service`] docs for information on this crate's foundational trait.
|
2020-08-09 16:10:58 +01:00
|
|
|
|
2020-12-27 14:15:42 +00:00
|
|
|
#![no_std]
|
2020-12-12 23:24:00 +00:00
|
|
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
2019-12-02 22:30:09 +06:00
|
|
|
#![allow(clippy::type_complexity)]
|
2020-12-12 23:24:00 +00:00
|
|
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
|
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
2019-12-02 22:30:09 +06:00
|
|
|
|
2020-12-27 14:15:42 +00:00
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
use alloc::{boxed::Box, rc::Rc, sync::Arc};
|
|
|
|
use core::{
|
|
|
|
cell::RefCell,
|
|
|
|
future::Future,
|
|
|
|
task::{self, Context, Poll},
|
|
|
|
};
|
2019-01-24 19:19:44 -08:00
|
|
|
|
2018-08-25 09:02:14 -07:00
|
|
|
mod and_then;
|
2018-08-30 09:17:17 -07:00
|
|
|
mod apply;
|
2019-03-09 09:01:02 -08:00
|
|
|
mod apply_cfg;
|
2019-02-22 18:20:54 -08:00
|
|
|
pub mod boxed;
|
2020-12-27 18:24:57 +00:00
|
|
|
mod ext;
|
2018-08-25 09:02:14 -07:00
|
|
|
mod fn_service;
|
2018-08-28 10:39:27 -07:00
|
|
|
mod map;
|
2019-05-12 06:03:50 -07:00
|
|
|
mod map_config;
|
2018-08-25 09:02:14 -07:00
|
|
|
mod map_err;
|
|
|
|
mod map_init_err;
|
2019-11-14 18:38:24 +06:00
|
|
|
mod pipeline;
|
2020-12-27 18:24:57 +00:00
|
|
|
mod ready;
|
2018-10-02 21:47:50 -07:00
|
|
|
mod then;
|
2019-02-03 10:42:27 -08:00
|
|
|
mod transform;
|
2019-12-03 18:32:02 +06:00
|
|
|
mod transform_err;
|
2019-03-12 12:53:08 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
|
|
|
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
2020-12-27 18:24:57 +00:00
|
|
|
pub use self::ext::{ServiceExt, ServiceFactoryExt};
|
2019-12-08 19:05:05 +06:00
|
|
|
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
|
2019-12-02 21:27:48 +06:00
|
|
|
pub use self::map_config::{map_config, unit_config};
|
2019-11-14 18:38:24 +06:00
|
|
|
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
|
|
|
pub use self::transform::{apply, Transform};
|
2019-05-12 06:03:50 -07:00
|
|
|
|
2020-12-27 18:24:57 +00:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use self::ready::{err, ok, ready, Ready};
|
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// 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:
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2021-01-09 14:13:16 +00:00
|
|
|
/// ```ignore
|
2020-08-09 16:10:58 +01:00
|
|
|
/// async fn(Request) -> Result<Response, Err>
|
|
|
|
/// ```
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2020-08-09 16:10:58 +01:00
|
|
|
/// 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.
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2020-08-09 16:10:58 +01:00
|
|
|
/// `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.
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2021-01-09 14:13:16 +00:00
|
|
|
/// ```ignore
|
2019-12-10 21:11:39 +06:00
|
|
|
/// struct MyService;
|
|
|
|
///
|
|
|
|
/// impl Service for MyService {
|
|
|
|
/// type Request = u8;
|
|
|
|
/// type Response = u64;
|
|
|
|
/// type Error = MyError;
|
2020-08-06 18:21:51 +08:00
|
|
|
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2021-01-22 19:06:22 -08:00
|
|
|
/// fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... }
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2021-01-22 19:06:22 -08:00
|
|
|
/// fn call(&self, req: Self::Request) -> Self::Future { ... }
|
2019-12-10 21:11:39 +06:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Sometimes it is not necessary to implement the Service trait. For example, the above service
|
2020-12-14 13:52:30 +05:30
|
|
|
/// could be rewritten as a simple function and passed to [fn_service](fn_service()).
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2021-01-09 14:13:16 +00:00
|
|
|
/// ```ignore
|
2019-12-10 21:11:39 +06:00
|
|
|
/// async fn my_service(req: u8) -> Result<u64, MyError>;
|
|
|
|
/// ```
|
2020-12-27 04:28:00 +00:00
|
|
|
pub trait Service<Req> {
|
2018-12-09 09:56:23 -08:00
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
/// Errors produced by the service when polling readiness or executing call.
|
2018-12-09 09:56:23 -08:00
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The future response value.
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future: Future<Output = Result<Self::Response, Self::Error>>;
|
2018-12-09 09:56:23 -08:00
|
|
|
|
|
|
|
/// Returns `Ready` when the service is able to process requests.
|
|
|
|
///
|
2020-01-11 23:44:01 +01:00
|
|
|
/// If the service is at capacity, then `Pending` is returned and the task
|
2018-12-09 09:56:23 -08:00
|
|
|
/// 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.
|
2019-12-10 21:11:39 +06:00
|
|
|
///
|
2020-08-09 16:10:58 +01:00
|
|
|
/// # Notes
|
2021-02-24 01:39:02 +00:00
|
|
|
/// 1. `poll_ready` might be called on a different task to `call`.
|
|
|
|
/// 1. In cases of chained services, `.poll_ready()` is called for all services at once.
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
|
2018-12-09 09:56:23 -08:00
|
|
|
|
|
|
|
/// 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.
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, req: Req) -> Self::Future;
|
2018-12-12 18:32:19 -08:00
|
|
|
}
|
2018-12-09 09:56:23 -08:00
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Factory for creating `Service`s.
|
2018-11-29 16:56:15 -10:00
|
|
|
///
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Acts as a service factory. This is useful for cases where new `Service`s
|
|
|
|
/// must be produced. One case is a TCP server listener. The listener
|
|
|
|
/// accepts new TCP streams, obtains a new `Service` using the
|
|
|
|
/// `ServiceFactory` trait, and uses the new `Service` to process inbound
|
2018-11-29 16:56:15 -10:00
|
|
|
/// requests on that new TCP stream.
|
2019-02-22 12:44:37 -08:00
|
|
|
///
|
2019-03-09 06:36:23 -08:00
|
|
|
/// `Config` is a service factory configuration type.
|
2020-12-27 04:28:00 +00:00
|
|
|
pub trait ServiceFactory<Req> {
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Responses given by the created services.
|
2018-11-29 16:56:15 -10:00
|
|
|
type Response;
|
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Errors produced by the created services.
|
2018-11-29 16:56:15 -10:00
|
|
|
type Error;
|
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Service factory configuration.
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config;
|
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// The kind of `Service` created by this factory.
|
2020-12-27 04:28:00 +00:00
|
|
|
type Service: Service<Req, Response = Self::Response, Error = Self::Error>;
|
2018-11-29 16:56:15 -10:00
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Errors potentially raised while building a service.
|
2018-11-29 16:56:15 -10:00
|
|
|
type InitError;
|
|
|
|
|
|
|
|
/// The future of the `Service` instance.
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
2018-11-29 16:56:15 -10:00
|
|
|
|
2020-08-09 16:10:58 +01:00
|
|
|
/// Create and return a new service asynchronously.
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, cfg: Self::Config) -> Self::Future;
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<'a, S, Req> Service<Req> for &'a mut S
|
2018-12-09 09:56:23 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req> + 'a,
|
2018-12-09 09:56:23 -08:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 18:38:24 +06:00
|
|
|
(**self).poll_ready(ctx)
|
2018-12-09 09:56:23 -08:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, request: Req) -> S::Future {
|
2018-12-09 09:56:23 -08:00
|
|
|
(**self).call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> Service<Req> for Box<S>
|
2018-12-09 10:14:08 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req> + ?Sized,
|
2018-12-09 10:14:08 -08:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
2019-11-14 18:38:24 +06:00
|
|
|
(**self).poll_ready(ctx)
|
2018-12-09 10:14:08 -08:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, request: Req) -> S::Future {
|
2018-12-09 10:14:08 -08:00
|
|
|
(**self).call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> Service<Req> for RefCell<S>
|
2019-11-19 08:45:09 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req>,
|
2019-11-19 08:45:09 +06:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.borrow().poll_ready(ctx)
|
2019-11-19 08:45:09 +06:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, request: Req) -> S::Future {
|
|
|
|
self.borrow().call(request)
|
2019-11-19 08:45:09 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> Service<Req> for Rc<RefCell<S>>
|
2019-03-29 10:21:17 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req>,
|
2019-03-29 10:21:17 -07:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.borrow().poll_ready(ctx)
|
2019-03-29 10:21:17 -07:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, request: Req) -> S::Future {
|
|
|
|
self.borrow().call(request)
|
2019-03-29 10:21:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> ServiceFactory<Req> for Rc<S>
|
2019-02-19 11:31:54 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: ServiceFactory<Req>,
|
2019-02-19 11:31:54 -08:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = S::Config;
|
2019-02-19 11:31:54 -08:00
|
|
|
type Service = S::Service;
|
|
|
|
type InitError = S::InitError;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, cfg: S::Config) -> S::Future {
|
2019-02-22 12:44:37 -08:00
|
|
|
self.as_ref().new_service(cfg)
|
2019-02-19 11:31:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> ServiceFactory<Req> for Arc<S>
|
2019-02-19 11:31:54 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: ServiceFactory<Req>,
|
2019-02-19 11:31:54 -08:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = S::Config;
|
2019-02-19 11:31:54 -08:00
|
|
|
type Service = S::Service;
|
|
|
|
type InitError = S::InitError;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, cfg: S::Config) -> S::Future {
|
2019-02-22 12:44:37 -08:00
|
|
|
self.as_ref().new_service(cfg)
|
2019-02-19 11:31:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 09:57:47 -07:00
|
|
|
/// Trait for types that can be converted to a `Service`
|
2020-12-27 04:28:00 +00:00
|
|
|
pub trait IntoService<S, Req>
|
2018-09-04 09:49:21 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req>,
|
2018-09-04 09:49:21 -07:00
|
|
|
{
|
2018-09-04 09:57:47 -07:00
|
|
|
/// Convert to a `Service`
|
2020-12-27 04:28:00 +00:00
|
|
|
fn into_service(self) -> S;
|
2018-09-04 09:49:21 -07:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
/// Trait for types that can be converted to a `ServiceFactory`
|
2020-12-27 04:28:00 +00:00
|
|
|
pub trait IntoServiceFactory<SF, Req>
|
2018-09-04 09:49:21 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<Req>,
|
2018-09-04 09:49:21 -07:00
|
|
|
{
|
2019-12-10 21:11:39 +06:00
|
|
|
/// Convert `Self` to a `ServiceFactory`
|
2020-12-27 04:28:00 +00:00
|
|
|
fn into_factory(self) -> SF;
|
2018-09-04 09:49:21 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, Req> IntoService<S, Req> for S
|
2018-09-04 09:49:21 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<Req>,
|
2018-09-04 09:49:21 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
fn into_service(self) -> S {
|
2018-09-04 09:49:21 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, Req> IntoServiceFactory<SF, Req> for SF
|
2019-02-22 14:30:00 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<Req>,
|
2019-02-22 14:30:00 -08:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
fn into_factory(self) -> SF {
|
2019-02-22 14:30:00 -08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 14:30:04 +06:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
/// Convert object of type `U` to a service `S`
|
|
|
|
pub fn into_service<I, S, Req>(tp: I) -> S
|
2020-01-08 18:31:50 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
I: IntoService<S, Req>,
|
|
|
|
S: Service<Req>,
|
2020-01-08 18:31:50 +06:00
|
|
|
{
|
|
|
|
tp.into_service()
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:30:04 +06:00
|
|
|
pub mod dev {
|
2020-12-27 04:28:00 +00:00
|
|
|
pub use crate::apply::{Apply, ApplyFactory};
|
2019-11-18 14:30:04 +06:00
|
|
|
pub use crate::fn_service::{
|
|
|
|
FnService, FnServiceConfig, FnServiceFactory, FnServiceNoConfig,
|
|
|
|
};
|
|
|
|
pub use crate::map::{Map, MapServiceFactory};
|
2019-11-18 18:28:54 +06:00
|
|
|
pub use crate::map_config::{MapConfig, UnitConfig};
|
2019-11-18 14:30:04 +06:00
|
|
|
pub use crate::map_err::{MapErr, MapErrServiceFactory};
|
|
|
|
pub use crate::map_init_err::MapInitErr;
|
2019-11-19 06:51:43 +06:00
|
|
|
pub use crate::transform::ApplyTransform;
|
2019-12-03 18:32:02 +06:00
|
|
|
pub use crate::transform_err::TransformMapInitErr;
|
2019-11-18 14:30:04 +06:00
|
|
|
}
|
2020-12-27 14:15:42 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! always_ready {
|
|
|
|
() => {
|
2021-01-22 19:06:22 -08:00
|
|
|
#[inline]
|
2020-12-27 14:15:42 +00:00
|
|
|
fn poll_ready(
|
2021-01-22 19:06:22 -08:00
|
|
|
&self,
|
2020-12-27 14:15:42 +00:00
|
|
|
_: &mut ::core::task::Context<'_>,
|
|
|
|
) -> ::core::task::Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! forward_ready {
|
|
|
|
($field:ident) => {
|
2021-01-22 19:06:22 -08:00
|
|
|
#[inline]
|
2020-12-27 14:15:42 +00:00
|
|
|
fn poll_ready(
|
2021-01-22 19:06:22 -08:00
|
|
|
&self,
|
2020-12-27 14:15:42 +00:00
|
|
|
cx: &mut ::core::task::Context<'_>,
|
|
|
|
) -> ::core::task::Poll<Result<(), Self::Error>> {
|
2021-01-09 14:13:16 +00:00
|
|
|
self.$field
|
|
|
|
.poll_ready(cx)
|
|
|
|
.map_err(::core::convert::Into::into)
|
2020-12-27 14:15:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|