tower_limit/concurrency/
service.rsuse super::future::ResponseFuture;
use tower_service::Service;
use super::sync::semaphore::{self, Semaphore};
use futures_core::ready;
use std::sync::Arc;
use std::task::{Context, Poll};
#[derive(Debug)]
pub struct ConcurrencyLimit<T> {
inner: T,
limit: Limit,
}
#[derive(Debug)]
struct Limit {
semaphore: Arc<Semaphore>,
permit: semaphore::Permit,
}
impl<T> ConcurrencyLimit<T> {
pub fn new(inner: T, max: usize) -> Self {
ConcurrencyLimit {
inner,
limit: Limit {
semaphore: Arc::new(Semaphore::new(max)),
permit: semaphore::Permit::new(),
},
}
}
pub fn get_ref(&self) -> &T {
&self.inner
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
}
impl<S, Request> Service<Request> for ConcurrencyLimit<S>
where
S: Service<Request>,
{
type Response = S::Response;
type Error = S::Error;
type Future = ResponseFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.limit.permit.poll_acquire(cx, &self.limit.semaphore))
.expect("poll_acquire after semaphore closed ");
Poll::Ready(ready!(self.inner.poll_ready(cx)))
}
fn call(&mut self, request: Request) -> Self::Future {
if self
.limit
.permit
.try_acquire(&self.limit.semaphore)
.is_err()
{
panic!("max requests in-flight; poll_ready must be called first");
}
let future = self.inner.call(request);
self.limit.permit.forget();
ResponseFuture::new(future, self.limit.semaphore.clone())
}
}
impl<S> tower_load::Load for ConcurrencyLimit<S>
where
S: tower_load::Load,
{
type Metric = S::Metric;
fn load(&self) -> Self::Metric {
self.inner.load()
}
}
impl<S> Clone for ConcurrencyLimit<S>
where
S: Clone,
{
fn clone(&self) -> ConcurrencyLimit<S> {
ConcurrencyLimit {
inner: self.inner.clone(),
limit: Limit {
semaphore: self.limit.semaphore.clone(),
permit: semaphore::Permit::new(),
},
}
}
}
impl Drop for Limit {
fn drop(&mut self) {
self.permit.release(&self.semaphore);
}
}