Skip to content

Commit

Permalink
util: add CloneService
Browse files Browse the repository at this point in the history
This upstreams a little utility I'm using a bunch in axum. Its often
useful to erase the type of a service while still being able to clone
it.

`BoxService` isn't `Clone` previously you had to combine it with
`Buffer` but doing that a lot (which we did in axum) had measurable
impact on performance.
  • Loading branch information
davidpdrsn committed Nov 9, 2021
1 parent 0f16ea5 commit 9354ef2
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tower/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **util**: Add `CloneService` which is a `Clone + Send` boxed `Service`.

### Fixed

- **balance**: Remove redundant `Req: Clone` bound from `Clone` impls
Expand Down
138 changes: 138 additions & 0 deletions tower/src/util/clone_boxed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use super::ServiceExt;
use futures_util::future::BoxFuture;
use tower_layer::{LayerFn, layer_fn};
use std::{
fmt,
task::{Context, Poll},
};
use tower_service::Service;

/// A `Clone + Send` boxed `Service`.
///
/// [`CloneBoxService`] turns a service into a trait object, allowing the
/// response future type to be dynamic, and allowing the service to be cloned.
///
/// # Example
///
/// ```
/// use tower::{Service, ServiceBuilder, BoxError, util::CloneBoxService};
/// use std::time::Duration;
/// #
/// # struct Request;
/// # struct Response;
/// # impl Response {
/// # fn new() -> Self { Self }
/// # }
///
/// // This service has a complex type that is hard to name
/// let service = ServiceBuilder::new()
/// .map_request(|req| {
/// println!("received request");
/// req
/// })
/// .map_response(|res| {
/// println!("response produced");
/// res
/// })
/// .load_shed()
/// .concurrency_limit(64)
/// .timeout(Duration::from_secs(10))
/// .service_fn(|req: Request| async {
/// Ok::<_, BoxError>(Response::new())
/// });
/// # let service = assert_service(service);
///
/// // `CloneService` will erase the type so its nameable
/// let service: CloneBoxService<Request, Response, BoxError> = CloneBoxService::new(service);
/// # let service = assert_service(service);
///
/// // And we can still clone the service
/// let cloned_service = service.clone();
/// #
/// # fn assert_service<S, R>(svc: S) -> S
/// # where S: Service<R> { svc }
/// ```
pub struct CloneBoxService<T, U, E>(
Box<
dyn CloneService<T, Response = U, Error = E, Future = BoxFuture<'static, Result<U, E>>>
+ Send,
>,
);

impl<T, U, E> CloneBoxService<T, U, E> {
/// Create a new `CloneBoxService`.
pub fn new<S>(inner: S) -> Self
where
S: Service<T, Response = U, Error = E> + Clone + Send + 'static,
S::Future: Send + 'static,
{
let inner = inner.map_future(|f| Box::pin(f) as _);
CloneBoxService(Box::new(inner))
}

/// Returns a [`Layer`] for wrapping a [`Service`] in a [`CloneBoxService`]
/// middleware.
///
/// [`Layer`]: crate::Layer
pub fn layer<S>() -> LayerFn<fn(S) -> Self>
where
S: Service<T, Response = U, Error = E> + Clone + Send + 'static,
S::Future: Send + 'static,
{
layer_fn(Self::new)
}
}

impl<T, U, E> Service<T> for CloneBoxService<T, U, E> {
type Response = U;
type Error = E;
type Future = BoxFuture<'static, Result<U, E>>;

#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
self.0.poll_ready(cx)
}

#[inline]
fn call(&mut self, request: T) -> Self::Future {
self.0.call(request)
}
}

impl<T, U, E> Clone for CloneBoxService<T, U, E> {
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}

trait CloneService<R>: Service<R> {
fn clone_box(
&self,
) -> Box<
dyn CloneService<R, Response = Self::Response, Error = Self::Error, Future = Self::Future>
+ Send,
>;
}

impl<R, T> CloneService<R> for T
where
T: Service<R> + Send + Clone + 'static,
{
fn clone_box(
&self,
) -> Box<dyn CloneService<R, Response = T::Response, Error = T::Error, Future = T::Future> + Send>
{
Box::new(self.clone())
}
}

impl<T, U, E> fmt::Debug for CloneBoxService<T, U, E>
where
T: fmt::Debug,
U: fmt::Debug,
E: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("CloneBoxService").finish()
}
}
2 changes: 2 additions & 0 deletions tower/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod and_then;
mod boxed;
mod clone_boxed;
mod call_all;
mod either;

Expand All @@ -22,6 +23,7 @@ mod then;
pub use self::{
and_then::{AndThen, AndThenLayer},
boxed::{BoxLayer, BoxService, UnsyncBoxService},
clone_boxed::CloneBoxService,
either::Either,
future_service::{future_service, FutureService},
map_err::{MapErr, MapErrLayer},
Expand Down

0 comments on commit 9354ef2

Please sign in to comment.