relay_server/services/server/
concurrency_limit.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tokio_util::sync::PollSemaphore;
use tower::{Layer, Service};

/// Enforces a limit on the concurrent number of services that can be created by a [`tower::MakeService`].
#[derive(Debug, Clone)]
pub struct ConcurrencyLimitLayer {
    permits: Option<usize>,
}

impl ConcurrencyLimitLayer {
    pub fn new(permits: Option<usize>) -> Self {
        Self { permits }
    }
}

impl<S> Layer<S> for ConcurrencyLimitLayer {
    type Service = ConcurrencyLimit<S>;

    fn layer(&self, inner: S) -> Self::Service {
        ConcurrencyLimit::new(inner, self.permits)
    }
}

/// Enforces a concurrency limit on a [`tower::MakeService`].
///
/// The yielded service keeps the permit alive until it is discarded.
/// This limits the amount of concurrently created services by this [`tower::MakeService`].
pub struct ConcurrencyLimit<T> {
    inner: T,
    semaphore: Option<PollSemaphore>,
    permit: Option<Permit>,
}

impl<T> ConcurrencyLimit<T> {
    pub fn new(inner: T, permits: Option<usize>) -> Self {
        Self {
            inner,
            semaphore: permits.map(|permits| PollSemaphore::new(Arc::new(Semaphore::new(permits)))),
            permit: None,
        }
    }
}

impl<S, Request> Service<Request> for ConcurrencyLimit<S>
where
    S: Service<Request>,
{
    type Response = Permitted<S::Response>;
    type Error = S::Error;
    type Future = ResponseFuture<S::Future>;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        if self.permit.is_none() {
            self.permit = match self.semaphore.as_mut() {
                Some(semaphore) => {
                    let permit = futures::ready!(semaphore.poll_acquire(cx));
                    debug_assert!(
                        permit.is_some(),
                        "Semaphore is never closed, so `poll_acquire` should never fail"
                    );
                    permit
                        .map(Arc::new)
                        .map(|_permit| Permit::Permitted { _permit })
                }
                None => Some(Permit::Unlimited),
            }
        }

        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        let permit = self.permit.take().expect("poll_ready must be called first");

        let future = self.inner.call(req);
        ResponseFuture::new(future, permit)
    }
}

impl<T: Clone> Clone for ConcurrencyLimit<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            semaphore: self.semaphore.clone(),
            permit: None,
        }
    }
}

#[derive(Clone, Debug)]
enum Permit {
    Unlimited,
    Permitted { _permit: Arc<OwnedSemaphorePermit> },
}

pin_project_lite::pin_project! {
    /// Future for the [`ConcurrencyLimit`] service.
    #[derive(Debug)]
    pub struct ResponseFuture<T> {
        #[pin]
        inner: T,
        permit: Permit,
    }
}

impl<T> ResponseFuture<T> {
    fn new(inner: T, permit: Permit) -> ResponseFuture<T> {
        ResponseFuture { inner, permit }
    }
}

impl<F, T, E> Future for ResponseFuture<F>
where
    F: Future<Output = Result<T, E>>,
{
    type Output = Result<Permitted<T>, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        let inner = match futures::ready!(this.inner.poll(cx)) {
            Ok(inner) => inner,
            Err(err) => return Poll::Ready(Err(err)),
        };

        Poll::Ready(Ok(Permitted {
            inner,
            _permit: this.permit.clone(),
        }))
    }
}

/// A service holding a permit.
///
/// Clones of the service all share the same permit.
#[derive(Clone)]
pub struct Permitted<T> {
    inner: T,
    _permit: Permit,
}

impl<S, Request> Service<Request> for Permitted<S>
where
    S: Service<Request>,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = S::Future;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        self.inner.call(req)
    }
}