2019-04-11 13:38:45 -07:00
|
|
|
use super::ConcurrencyLimit;
|
2019-03-07 13:53:21 -08:00
|
|
|
use tower_layer::Layer;
|
|
|
|
|
2019-04-23 13:42:56 -07:00
|
|
|
/// Enforces a limit on the concurrent number of requests the underlying
|
|
|
|
/// service can handle.
|
2019-03-07 13:53:21 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2019-04-05 20:08:43 -07:00
|
|
|
pub struct ConcurrencyLimitLayer {
|
2019-03-07 13:53:21 -08:00
|
|
|
max: usize,
|
|
|
|
}
|
|
|
|
|
2019-04-05 20:08:43 -07:00
|
|
|
impl ConcurrencyLimitLayer {
|
2019-04-23 13:42:56 -07:00
|
|
|
/// Create a new concurrency limit layer.
|
2019-03-07 13:53:21 -08:00
|
|
|
pub fn new(max: usize) -> Self {
|
2019-04-05 20:08:43 -07:00
|
|
|
ConcurrencyLimitLayer { max }
|
2019-03-07 13:53:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:38:45 -07:00
|
|
|
impl<S> Layer<S> for ConcurrencyLimitLayer {
|
2019-04-05 20:08:43 -07:00
|
|
|
type Service = ConcurrencyLimit<S>;
|
2019-03-07 13:53:21 -08:00
|
|
|
|
2019-04-11 13:38:45 -07:00
|
|
|
fn layer(&self, service: S) -> Self::Service {
|
|
|
|
ConcurrencyLimit::new(service, self.max)
|
2019-03-07 13:53:21 -08:00
|
|
|
}
|
|
|
|
}
|