tower_acc/layer.rs
1use tower_layer::Layer;
2
3use crate::Algorithm;
4use crate::service::ConcurrencyLimit;
5
6/// A [`Layer`] that wraps services with an adaptive [`ConcurrencyLimit`].
7///
8/// # Example
9///
10/// ```rust,no_run
11/// use tower::ServiceBuilder;
12/// use tower_acc::{ConcurrencyLimitLayer, Vegas};
13/// # fn wrap<S>(my_service: S) -> impl tower_service::Service<()>
14/// # where S: tower_service::Service<(), Error = std::convert::Infallible> {
15///
16/// let service = ServiceBuilder::new()
17/// .layer(ConcurrencyLimitLayer::new(Vegas::default()))
18/// .service(my_service);
19/// # service
20/// # }
21/// ```
22#[derive(Debug, Clone)]
23pub struct ConcurrencyLimitLayer<A> {
24 algorithm: A,
25}
26
27impl<A> ConcurrencyLimitLayer<A> {
28 /// Creates a new `ConcurrencyLimitLayer` with the given algorithm.
29 pub fn new(algorithm: A) -> Self {
30 Self { algorithm }
31 }
32}
33
34impl<S, A> Layer<S> for ConcurrencyLimitLayer<A>
35where
36 A: Algorithm + Clone,
37{
38 type Service = ConcurrencyLimit<S, A>;
39
40 fn layer(&self, service: S) -> Self::Service {
41 ConcurrencyLimit::new(service, self.algorithm.clone())
42 }
43}