Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/layered/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ allowed_external_types = ["tower_layer::Layer", "tower_service::Service"]
[features]
default = []
intercept = []
dynamic-service = ["dep:dynosaur"]
dynamic-service = ["dep:infinity_pool"]
tower-service = ["dep:tower-service"]

[dependencies]
dynosaur = { workspace = true, optional = true }
infinity_pool = { workspace = true, optional = true }
tower-layer = { workspace = true }
tower-service = { workspace = true, optional = true }

[dev-dependencies]
alloc_tracker = { workspace = true }
criterion = { workspace = true }
dynosaur = { workspace = true }
futures = { workspace = true, features = ["executor"] }
infinity_pool = { workspace = true }
mutants = { workspace = true }
pin-project-lite = { workspace = true }
static_assertions = { workspace = true }
Expand Down
47 changes: 41 additions & 6 deletions crates/layered/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Licensed under the MIT License.

use std::fmt::Debug;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use infinity_pool::{BlindPool, BlindPooledMut, define_pooled_dyn_cast};

use crate::Service;
use crate::service::DynService;

/// Extension trait for converting services to [`DynamicService`].
///
Expand Down Expand Up @@ -51,22 +54,52 @@ where
/// println!("Result: {}", result);
/// }
/// ```
pub struct DynamicService<In, Out>(Arc<DynService<'static, In, Out>>);
pub struct DynamicService<In, Out> {
exec: Arc<dyn Fn(In) -> BlindPooledMut<dyn SendFuture<Out>> + Send + Sync>,
}

pub(crate) trait SendFuture<Out>: Future<Output = Out> + Send {}
impl<Out: Send + 'static, F: Future<Output = Out> + Send> SendFuture<Out> for F {}
define_pooled_dyn_cast!(SendFuture<Out>);

impl<In: Send + 'static, Out: Send + 'static> DynamicService<In, Out> {
pub(crate) fn new<T>(strategy: T) -> Self
where
T: Service<In, Out = Out> + Send + Sync + 'static,
{
Self(DynService::new_arc(strategy))
let pool = BlindPool::new();
let service = Arc::new(strategy);
// define a delegate that wraps the service execution in a
// future and inserts it into the pool
let exec = move |input: In| {
let cloned = Arc::clone(&service);
let fut = async move { cloned.execute(input).await };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generics are blinding my eyes today, so I cannot identify it myself but is it possible here to get a type for the thing being inserted, or at least a Layout? If so, switching to OpaquePool would eliminate some overhead. OpaquePool vs BlindPool is basically "one type/layout" versus "multiple types/layouts", with the BlindPool acting as a router over a pool of OpaquePool instances.

pool.insert(fut).cast_send_future()
};

Self { exec: Arc::new(exec) }
}
}

impl<In: Send, Out: Send> Service<In> for DynamicService<In, Out> {
type Out = Out;

async fn execute(&self, input: In) -> Self::Out {
self.0.execute(input).await
fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send {
ServiceFuture {
handle: self.exec.as_ref()(input),
}
}
}
Comment on lines 84 to +92

struct ServiceFuture<Out> {
handle: BlindPooledMut<dyn SendFuture<Out>>,
}

impl<Out> Future for ServiceFuture<Out> {
type Output = Out;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().handle.as_pin_mut().poll(cx)
}
}

Expand All @@ -78,7 +111,9 @@ impl<In, Out> Debug for DynamicService<In, Out> {

impl<In, Out> Clone for DynamicService<In, Out> {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
Self {
exec: Arc::clone(&self.exec),
}
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/layered/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/// to wrap closures.
///
/// See the [crate documentation][crate] for usage examples and layer composition.
#[cfg_attr(any(test, feature = "dynamic-service"), dynosaur::dynosaur(pub DynService = dyn(box) Service, bridge(none)))]
pub trait Service<In>: Send + Sync {
/// The output type returned by this service.
type Out;
Expand Down
Loading