1 use super::{Rate, RateLimit}; 2 use std::time::Duration; 3 use 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, Clone)] 8 pub struct RateLimitLayer { 9 rate: Rate, 10 } 11 12 impl RateLimitLayer { 13 /// Create new rate limit layer. new(num: u64, per: Duration) -> Self14 pub fn new(num: u64, per: Duration) -> Self { 15 let rate = Rate::new(num, per); 16 RateLimitLayer { rate } 17 } 18 } 19 20 impl<S> Layer<S> for RateLimitLayer { 21 type Service = RateLimit<S>; 22 layer(&self, service: S) -> Self::Service23 fn layer(&self, service: S) -> Self::Service { 24 RateLimit::new(service, self.rate) 25 } 26 } 27