1use std::sync::Arc;
23use super::ConcurrencyLimit;
4use tokio::sync::Semaphore;
5use tower_layer::Layer;
67/// Enforces a limit on the concurrent number of requests the underlying
8/// service can handle.
9#[derive(Debug, Clone)]
10pub struct ConcurrencyLimitLayer {
11 max: usize,
12}
1314impl ConcurrencyLimitLayer {
15/// Create a new concurrency limit layer.
16pub fn new(max: usize) -> Self {
17 ConcurrencyLimitLayer { max }
18 }
19}
2021impl<S> Layer<S> for ConcurrencyLimitLayer {
22type Service = ConcurrencyLimit<S>;
2324fn layer(&self, service: S) -> Self::Service {
25 ConcurrencyLimit::new(service, self.max)
26 }
27}
2829/// Enforces a limit on the concurrent number of requests the underlying
30/// service can handle.
31///
32/// Unlike [`ConcurrencyLimitLayer`], which enforces a per-service concurrency
33/// limit, this layer accepts a owned semaphore (`Arc<Semaphore>`) which can be
34/// shared across multiple services.
35///
36/// Cloning this layer will not create a new semaphore.
37#[derive(Debug, Clone)]
38pub struct GlobalConcurrencyLimitLayer {
39 semaphore: Arc<Semaphore>,
40}
4142impl GlobalConcurrencyLimitLayer {
43/// Create a new `GlobalConcurrencyLimitLayer`.
44pub fn new(max: usize) -> Self {
45Self::with_semaphore(Arc::new(Semaphore::new(max)))
46 }
4748/// Create a new `GlobalConcurrencyLimitLayer` from a `Arc<Semaphore>`
49pub fn with_semaphore(semaphore: Arc<Semaphore>) -> Self {
50 GlobalConcurrencyLimitLayer { semaphore }
51 }
52}
5354impl<S> Layer<S> for GlobalConcurrencyLimitLayer {
55type Service = ConcurrencyLimit<S>;
5657fn layer(&self, service: S) -> Self::Service {
58 ConcurrencyLimit::with_semaphore(service, self.semaphore.clone())
59 }
60}