tower_limit/rate/
layer.rs

1use super::{Rate, RateLimit};
2use std::time::Duration;
3use tower_layer::Layer;
4
5/// Enforces a rate limit on the number of requests the underlying
6/// service can handle over a period of time.
7#[derive(Debug)]
8pub struct RateLimitLayer {
9    rate: Rate,
10}
11
12impl RateLimitLayer {
13    /// Create new rate limit layer.
14    pub fn new(num: u64, per: Duration) -> Self {
15        let rate = Rate::new(num, per);
16        RateLimitLayer { rate }
17    }
18}
19
20impl<S> Layer<S> for RateLimitLayer {
21    type Service = RateLimit<S>;
22
23    fn layer(&self, service: S) -> Self::Service {
24        RateLimit::new(service, self.rate)
25    }
26}