tower_async/limit/
layer.rs

1use super::Limit;
2use tower_async_layer::Layer;
3
4/// Limit requests based on a policy
5#[derive(Debug)]
6pub struct LimitLayer<P> {
7    policy: P,
8}
9
10impl<P> LimitLayer<P> {
11    /// Creates a new [`LimitLayer`] from a [`crate::limit::Policy`].
12    pub fn new(policy: P) -> Self {
13        LimitLayer { policy }
14    }
15}
16
17impl<T, P> Layer<T> for LimitLayer<P>
18where
19    P: Clone,
20{
21    type Service = Limit<T, P>;
22
23    fn layer(&self, service: T) -> Self::Service {
24        let policy = self.policy.clone();
25        Limit::new(service, policy)
26    }
27}