opendal/layers/
concurrent_limit.rs1use std::fmt::Debug;
19use std::pin::Pin;
20use std::sync::Arc;
21use std::task::Context;
22use std::task::Poll;
23
24use futures::Stream;
25use futures::StreamExt;
26use mea::semaphore::OwnedSemaphorePermit;
27use mea::semaphore::Semaphore;
28
29use crate::raw::*;
30use crate::*;
31
32#[derive(Clone)]
84pub struct ConcurrentLimitLayer {
85 operation_semaphore: Arc<Semaphore>,
86 http_semaphore: Option<Arc<Semaphore>>,
87}
88
89impl ConcurrentLimitLayer {
90 pub fn new(permits: usize) -> Self {
94 Self {
95 operation_semaphore: Arc::new(Semaphore::new(permits)),
96 http_semaphore: None,
97 }
98 }
99
100 pub fn with_http_concurrent_limit(mut self, permits: usize) -> Self {
105 self.http_semaphore = Some(Arc::new(Semaphore::new(permits)));
106 self
107 }
108}
109
110impl<A: Access> Layer<A> for ConcurrentLimitLayer {
111 type LayeredAccess = ConcurrentLimitAccessor<A>;
112
113 fn layer(&self, inner: A) -> Self::LayeredAccess {
114 let info = inner.info();
115
116 info.update_http_client(|client| {
118 HttpClient::with(ConcurrentLimitHttpFetcher {
119 inner: client.into_inner(),
120 http_semaphore: self.http_semaphore.clone(),
121 })
122 });
123
124 ConcurrentLimitAccessor {
125 inner,
126 semaphore: self.operation_semaphore.clone(),
127 }
128 }
129}
130
131pub struct ConcurrentLimitHttpFetcher {
132 inner: HttpFetcher,
133 http_semaphore: Option<Arc<Semaphore>>,
134}
135
136impl HttpFetch for ConcurrentLimitHttpFetcher {
137 async fn fetch(&self, req: http::Request<Buffer>) -> Result<http::Response<HttpBody>> {
138 let Some(semaphore) = self.http_semaphore.clone() else {
139 return self.inner.fetch(req).await;
140 };
141
142 let permit = semaphore.acquire_owned(1).await;
143
144 let resp = self.inner.fetch(req).await?;
145 let (parts, body) = resp.into_parts();
146 let body = body.map_inner(|s| {
147 Box::new(ConcurrentLimitStream {
148 inner: s,
149 _permit: permit,
150 })
151 });
152 Ok(http::Response::from_parts(parts, body))
153 }
154}
155
156pub struct ConcurrentLimitStream<S> {
157 inner: S,
158 _permit: OwnedSemaphorePermit,
160}
161
162impl<S> Stream for ConcurrentLimitStream<S>
163where
164 S: Stream<Item = Result<Buffer>> + Unpin + 'static,
165{
166 type Item = Result<Buffer>;
167
168 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
169 self.inner.poll_next_unpin(cx)
170 }
171}
172
173#[derive(Debug, Clone)]
174pub struct ConcurrentLimitAccessor<A: Access> {
175 inner: A,
176 semaphore: Arc<Semaphore>,
177}
178
179impl<A: Access> LayeredAccess for ConcurrentLimitAccessor<A> {
180 type Inner = A;
181 type Reader = ConcurrentLimitWrapper<A::Reader>;
182 type Writer = ConcurrentLimitWrapper<A::Writer>;
183 type Lister = ConcurrentLimitWrapper<A::Lister>;
184 type Deleter = ConcurrentLimitWrapper<A::Deleter>;
185
186 fn inner(&self) -> &Self::Inner {
187 &self.inner
188 }
189
190 async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
191 let _permit = self.semaphore.acquire(1).await;
192
193 self.inner.create_dir(path, args).await
194 }
195
196 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
197 let permit = self.semaphore.clone().acquire_owned(1).await;
198
199 self.inner
200 .read(path, args)
201 .await
202 .map(|(rp, r)| (rp, ConcurrentLimitWrapper::new(r, permit)))
203 }
204
205 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
206 let permit = self.semaphore.clone().acquire_owned(1).await;
207
208 self.inner
209 .write(path, args)
210 .await
211 .map(|(rp, w)| (rp, ConcurrentLimitWrapper::new(w, permit)))
212 }
213
214 async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
215 let _permit = self.semaphore.acquire(1).await;
216
217 self.inner.stat(path, args).await
218 }
219
220 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
221 let permit = self.semaphore.clone().acquire_owned(1).await;
222
223 self.inner
224 .delete()
225 .await
226 .map(|(rp, w)| (rp, ConcurrentLimitWrapper::new(w, permit)))
227 }
228
229 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
230 let permit = self.semaphore.clone().acquire_owned(1).await;
231
232 self.inner
233 .list(path, args)
234 .await
235 .map(|(rp, s)| (rp, ConcurrentLimitWrapper::new(s, permit)))
236 }
237}
238
239pub struct ConcurrentLimitWrapper<R> {
240 inner: R,
241
242 _permit: OwnedSemaphorePermit,
244}
245
246impl<R> ConcurrentLimitWrapper<R> {
247 fn new(inner: R, permit: OwnedSemaphorePermit) -> Self {
248 Self {
249 inner,
250 _permit: permit,
251 }
252 }
253}
254
255impl<R: oio::Read> oio::Read for ConcurrentLimitWrapper<R> {
256 async fn read(&mut self) -> Result<Buffer> {
257 self.inner.read().await
258 }
259}
260
261impl<R: oio::Write> oio::Write for ConcurrentLimitWrapper<R> {
262 async fn write(&mut self, bs: Buffer) -> Result<()> {
263 self.inner.write(bs).await
264 }
265
266 async fn close(&mut self) -> Result<Metadata> {
267 self.inner.close().await
268 }
269
270 async fn abort(&mut self) -> Result<()> {
271 self.inner.abort().await
272 }
273}
274
275impl<R: oio::List> oio::List for ConcurrentLimitWrapper<R> {
276 async fn next(&mut self) -> Result<Option<oio::Entry>> {
277 self.inner.next().await
278 }
279}
280
281impl<R: oio::Delete> oio::Delete for ConcurrentLimitWrapper<R> {
282 async fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
283 self.inner.delete(path, args).await
284 }
285
286 async fn close(&mut self) -> Result<()> {
287 self.inner.close().await
288 }
289}