1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-03 00:22:23 +01:00
actix-net/actix-service/src/transform.rs

272 lines
7.5 KiB
Rust
Raw Normal View History

2021-01-26 09:05:19 +01:00
use alloc::{rc::Rc, sync::Arc};
2020-12-27 15:15:42 +01:00
use core::{
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use futures_core::ready;
2020-12-27 15:15:42 +01:00
use pin_project_lite::pin_project;
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 13:38:24 +01:00
use crate::{IntoServiceFactory, Service, ServiceFactory};
/// Apply a [`Transform`] to a [`Service`].
pub fn apply<T, S, I, Req>(t: T, factory: I) -> ApplyTransform<T, S, Req>
2019-11-20 19:35:44 +01:00
where
I: IntoServiceFactory<S, Req>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
2019-11-20 19:35:44 +01:00
{
2019-12-11 05:29:34 +01:00
ApplyTransform::new(t, factory.into_factory())
2019-11-20 19:35:44 +01:00
}
2019-12-10 16:11:39 +01:00
/// The `Transform` trait defines the interface of a service factory that wraps inner service
/// during construction.
///
/// Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in
/// the request/response lifecycle. It may modify request and/or response.
2019-12-10 16:11:39 +01:00
///
/// For example, a timeout service wrapper:
2019-12-10 16:11:39 +01:00
///
/// ```ignore
2019-12-10 16:11:39 +01:00
/// pub struct Timeout<S> {
/// service: S,
/// timeout: Duration,
/// }
///
/// impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
2019-12-10 16:11:39 +01:00
/// type Response = S::Response;
/// type Error = TimeoutError<S::Error>;
/// type Future = TimeoutServiceResponse<S>;
///
/// actix_service::forward_ready!(service);
2019-12-10 16:11:39 +01:00
///
/// fn call(&self, req: S::Request) -> Self::Future {
2019-12-10 16:11:39 +01:00
/// TimeoutServiceResponse {
/// fut: self.service.call(req),
/// sleep: Sleep::new(clock::now() + self.timeout),
2019-12-10 16:11:39 +01:00
/// }
/// }
/// }
/// ```
///
/// This wrapper service is decoupled from the underlying service implementation and could be
/// applied to any service.
2019-12-10 16:11:39 +01:00
///
/// The `Transform` trait defines the interface of a service wrapper. `Transform` is often
/// implemented for middleware, defining how to construct a middleware Service. A Service that is
/// constructed by the factory takes the Service that follows it during execution as a parameter,
/// assuming ownership of the next Service.
2019-12-10 16:11:39 +01:00
///
/// A transform for the `Timeout` middleware could look like this:
2019-12-10 16:11:39 +01:00
///
/// ```ignore
2019-12-10 16:11:39 +01:00
/// pub struct TimeoutTransform {
/// timeout: Duration,
/// }
///
/// impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
2019-12-10 16:11:39 +01:00
/// type Response = S::Response;
/// type Error = TimeoutError<S::Error>;
/// type InitError = S::Error;
/// type Transform = Timeout<S>;
/// type Future = Ready<Result<Self::Transform, Self::InitError>>;
///
/// fn new_transform(&self, service: S) -> Self::Future {
/// ready(Ok(Timeout {
2019-12-10 16:11:39 +01:00
/// service,
/// timeout: self.timeout,
/// }))
2019-12-10 16:11:39 +01:00
/// }
/// }
/// ```
pub trait Transform<S, Req> {
/// Responses produced by the service.
type Response;
/// Errors produced by the service.
type Error;
/// The `TransformService` value created by this factory
type Transform: Service<Req, Response = Self::Response, Error = Self::Error>;
2019-12-10 16:11:39 +01:00
/// Errors produced while building a transform service.
type InitError;
/// The future response value.
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 13:38:24 +01:00
type Future: Future<Output = Result<Self::Transform, Self::InitError>>;
2019-12-10 16:11:39 +01:00
/// Creates and returns a new Transform component, asynchronously
2019-03-05 04:38:11 +01:00
fn new_transform(&self, service: S) -> Self::Future;
}
impl<T, S, Req> Transform<S, Req> for Rc<T>
where
T: Transform<S, Req>,
{
type Response = T::Response;
type Error = T::Error;
2019-03-05 04:38:11 +01:00
type Transform = T::Transform;
type InitError = T::InitError;
type Future = T::Future;
2019-03-05 04:38:11 +01:00
fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(service)
}
}
impl<T, S, Req> Transform<S, Req> for Arc<T>
where
T: Transform<S, Req>,
{
type Response = T::Response;
type Error = T::Error;
2019-03-05 04:38:11 +01:00
type Transform = T::Transform;
type InitError = T::InitError;
type Future = T::Future;
2019-03-05 04:38:11 +01:00
fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(service)
}
}
/// Apply a [`Transform`] to a [`Service`].
pub struct ApplyTransform<T, S, Req>(Rc<(T, S)>, PhantomData<Req>);
2019-03-05 06:24:47 +01:00
impl<T, S, Req> ApplyTransform<T, S, Req>
2019-03-05 06:24:47 +01:00
where
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
2019-03-12 20:53:08 +01:00
/// Create new `ApplyTransform` new service instance
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 13:38:24 +01:00
fn new(t: T, service: S) -> Self {
Self(Rc::new((t, service)), PhantomData)
2019-03-05 06:24:47 +01:00
}
}
impl<T, S, Req> Clone for ApplyTransform<T, S, Req> {
2019-03-12 20:53:08 +01:00
fn clone(&self) -> Self {
ApplyTransform(self.0.clone(), PhantomData)
2019-03-12 20:53:08 +01:00
}
}
impl<T, S, Req> ServiceFactory<Req> for ApplyTransform<T, S, Req>
2019-03-05 06:24:47 +01:00
where
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
type Response = T::Response;
type Error = T::Error;
type Config = S::Config;
2019-03-05 06:24:47 +01:00
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S, Req>;
2019-03-05 06:24:47 +01:00
fn new_service(&self, cfg: S::Config) -> Self::Future {
2019-03-05 06:24:47 +01:00
ApplyTransformFuture {
store: self.0.clone(),
2020-12-27 15:15:42 +01:00
state: ApplyTransformFutureState::A {
fut: self.0.as_ref().1.new_service(cfg),
},
2019-03-05 06:24:47 +01:00
}
}
}
2020-12-27 15:15:42 +01:00
pin_project! {
pub struct ApplyTransformFuture<T, S, Req>
where
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
store: Rc<(T, S)>,
#[pin]
state: ApplyTransformFutureState<T, S, Req>,
}
}
2020-12-27 15:15:42 +01:00
pin_project! {
#[project = ApplyTransformFutureStateProj]
pub enum ApplyTransformFutureState<T, S, Req>
where
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
A { #[pin] fut: S::Future },
B { #[pin] fut: T::Future },
}
2019-03-05 06:24:47 +01:00
}
impl<T, S, Req> Future for ApplyTransformFuture<T, S, Req>
2019-03-05 06:24:47 +01:00
where
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 13:38:24 +01:00
type Output = Result<T::Transform, T::InitError>;
2019-03-05 06:24:47 +01:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 13:38:24 +01:00
match this.state.as_mut().project() {
ApplyTransformFutureStateProj::A { fut } => {
let srv = ready!(fut.poll(cx))?;
let fut = this.store.0.new_transform(srv);
this.state.set(ApplyTransformFutureState::B { fut });
self.poll(cx)
}
2020-12-27 15:15:42 +01:00
ApplyTransformFutureStateProj::B { fut } => fut.poll(cx),
2019-03-05 06:24:47 +01:00
}
}
}
#[cfg(test)]
mod tests {
2021-04-15 21:43:02 +02:00
use core::time::Duration;
use actix_utils::future::{ready, Ready};
use super::*;
use crate::Service;
// pseudo-doctest for Transform trait
pub struct TimeoutTransform {
timeout: Duration,
}
// pseudo-doctest for Transform trait
impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
type Response = S::Response;
type Error = S::Error;
type InitError = S::Error;
type Transform = Timeout<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(Timeout {
service,
_timeout: self.timeout,
}))
}
}
// pseudo-doctest for Transform trait
pub struct Timeout<S> {
service: S,
_timeout: Duration,
}
// pseudo-doctest for Transform trait
impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
crate::forward_ready!(service);
fn call(&self, req: Req) -> Self::Future {
self.service.call(req)
}
}
}