Skip to main content

opendal_layer_throttle/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#![doc = include_str!("../README.md")]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![cfg_attr(docsrs, doc(auto_cfg))]
21#![deny(missing_docs)]
22use std::num::NonZeroU32;
23use std::sync::Arc;
24
25use governor::Quota;
26use governor::RateLimiter;
27use governor::clock::DefaultClock;
28use governor::middleware::NoOpMiddleware;
29use governor::state::InMemoryState;
30use governor::state::NotKeyed;
31use opendal_core::raw::*;
32use opendal_core::*;
33
34/// `ThrottleLayer` limits bandwidth for storage services.
35///
36/// # Throttle
37///
38/// This layer uses the Generic Cell Rate Algorithm (GCRA) from
39/// [Governor](https://docs.rs/governor/latest/governor/index.html).
40/// Set `bandwidth` and `burst` to control the service's byte-flow rate.
41///
42/// # Note
43///
44/// When setting the ThrottleLayer, always consider the largest possible operation size as the burst size,
45/// as **the burst size should be larger than any possible byte length to allow it to pass through**.
46///
47/// Read more about [Quota](https://docs.rs/governor/latest/governor/struct.Quota.html#examples)
48///
49/// # Examples
50///
51/// This example limits bandwidth to 10 KiB/s and burst size to 10 MiB.
52///
53/// ```no_run
54/// # use opendal_core::services;
55/// # use opendal_core::Operator;
56/// # use opendal_core::Result;
57/// # use opendal_layer_throttle::ThrottleLayer;
58/// #
59/// # fn main() -> Result<()> {
60/// let _ = Operator::new(services::Memory::default())
61///     .expect("must init")
62///     .layer(ThrottleLayer::new(10 * 1024, 10000 * 1024));
63/// # Ok(())
64/// # }
65/// ```
66#[derive(Clone, Debug)]
67pub struct ThrottleLayer {
68    rate_limiter: SharedRateLimiter,
69}
70
71impl ThrottleLayer {
72    /// Create a new `ThrottleLayer` with given bandwidth and burst.
73    ///
74    /// - bandwidth: the maximum number of bytes allowed to pass through per second.
75    /// - burst: the maximum number of bytes allowed to pass through at once.
76    pub fn new(bandwidth: u32, burst: u32) -> Self {
77        assert!(bandwidth > 0);
78        assert!(burst > 0);
79        Self {
80            rate_limiter: Arc::new(RateLimiter::direct(
81                Quota::per_second(NonZeroU32::new(bandwidth).unwrap())
82                    .allow_burst(NonZeroU32::new(burst).unwrap()),
83            )),
84        }
85    }
86}
87
88impl Layer for ThrottleLayer {
89    fn apply_service(&self, inner: Servicer) -> Servicer {
90        Arc::new(self.layer(inner))
91    }
92}
93
94impl ThrottleLayer {
95    fn layer(&self, inner: Servicer) -> ThrottleAccessor {
96        ThrottleAccessor {
97            inner,
98            rate_limiter: self.rate_limiter.clone(),
99        }
100    }
101}
102
103/// Share an atomic RateLimiter instance across all threads in one operator.
104/// If want to add more observability in the future, replace the default NoOpMiddleware with other middleware types.
105/// Read more about [Middleware](https://docs.rs/governor/latest/governor/middleware/index.html)
106type SharedRateLimiter = Arc<RateLimiter<NotKeyed, InMemoryState, DefaultClock, NoOpMiddleware>>;
107
108#[doc(hidden)]
109#[derive(Debug)]
110pub struct ThrottleAccessor {
111    inner: Servicer,
112    rate_limiter: SharedRateLimiter,
113}
114
115impl Service for ThrottleAccessor {
116    type Reader = ThrottleWrapper<oio::Reader>;
117    type Writer = ThrottleWrapper<oio::Writer>;
118    type Lister = oio::Lister;
119    type Deleter = oio::Deleter;
120    type Copier = oio::Copier;
121    type Composer = oio::Composer;
122
123    fn info(&self) -> ServiceInfo {
124        self.inner.info()
125    }
126
127    fn capability(&self) -> Capability {
128        self.inner.capability()
129    }
130
131    fn compose(&self, ctx: &OperationContext, to: &str, args: OpCompose) -> Result<Self::Composer> {
132        self.inner.compose(ctx, to, args)
133    }
134
135    async fn create_dir(
136        &self,
137        ctx: &OperationContext,
138        path: &str,
139        args: OpCreateDir,
140    ) -> Result<RpCreateDir> {
141        self.inner.create_dir(ctx, path, args).await
142    }
143
144    async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
145        self.inner.stat(ctx, path, args).await
146    }
147
148    fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
149        let limiter = self.rate_limiter.clone();
150
151        self.inner
152            .read(ctx, path, args)
153            .map(|r| ThrottleWrapper::new(r, limiter))
154    }
155
156    fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
157        let limiter = self.rate_limiter.clone();
158
159        self.inner
160            .write(ctx, path, args)
161            .map(|w| ThrottleWrapper::new(w, limiter))
162    }
163
164    fn copy(
165        &self,
166        ctx: &OperationContext,
167        from: &str,
168        to: &str,
169        args: OpCopy,
170    ) -> Result<Self::Copier> {
171        self.inner.copy(ctx, from, to, args)
172    }
173
174    fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
175        self.inner.delete(ctx)
176    }
177
178    async fn rename(
179        &self,
180        ctx: &OperationContext,
181        from: &str,
182        to: &str,
183        args: OpRename,
184    ) -> Result<RpRename> {
185        self.inner.rename(ctx, from, to, args).await
186    }
187
188    async fn restore(
189        &self,
190        ctx: &OperationContext,
191        path: &str,
192        args: OpRestore,
193    ) -> Result<RpRestore> {
194        self.inner.restore(ctx, path, args).await
195    }
196
197    fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
198        self.inner.list(ctx, path, args)
199    }
200
201    async fn presign(
202        &self,
203        ctx: &OperationContext,
204        path: &str,
205        args: OpPresign,
206    ) -> Result<RpPresign> {
207        self.inner.presign(ctx, path, args).await
208    }
209}
210
211#[doc(hidden)]
212pub struct ThrottleWrapper<R> {
213    inner: R,
214    limiter: SharedRateLimiter,
215}
216
217impl<R> ThrottleWrapper<R> {
218    fn new(inner: R, rate_limiter: SharedRateLimiter) -> Self {
219        Self {
220            inner,
221            limiter: rate_limiter,
222        }
223    }
224}
225
226impl<R: oio::ReadStream> oio::ReadStream for ThrottleWrapper<R> {
227    async fn read(&mut self) -> Result<Buffer> {
228        self.inner.read().await
229    }
230}
231
232impl<R: oio::Read> oio::Read for ThrottleWrapper<R> {
233    async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn oio::ReadStreamDyn>)> {
234        let (rp, stream) = self.inner.open(range).await?;
235        Ok((
236            rp,
237            Box::new(ThrottleWrapper::new(stream, self.limiter.clone()))
238                as Box<dyn oio::ReadStreamDyn>,
239        ))
240    }
241
242    async fn read(&self, range: BytesRange) -> Result<(RpRead, Buffer)> {
243        self.inner.read(range).await
244    }
245}
246
247impl<R: oio::Write> oio::Write for ThrottleWrapper<R> {
248    async fn write(&mut self, bs: Buffer) -> Result<()> {
249        let len = bs.len();
250        if len == 0 {
251            return self.inner.write(bs).await;
252        }
253
254        if len > u32::MAX as usize {
255            return Err(Error::new(
256                ErrorKind::RateLimited,
257                "request size exceeds throttle quota capacity",
258            ));
259        }
260
261        let buf_length =
262            NonZeroU32::new(len as u32).expect("len is non-zero so NonZeroU32 must exist");
263
264        self.limiter.until_n_ready(buf_length).await.map_err(|_| {
265            Error::new(
266                ErrorKind::RateLimited,
267                "burst size is smaller than the request size",
268            )
269        })?;
270
271        self.inner.write(bs).await
272    }
273
274    async fn copy_from(&mut self, path: &str, args: OpRead, range: BytesRange) -> Result<()> {
275        self.inner.copy_from(path, args, range).await
276    }
277
278    async fn close(&mut self) -> Result<Metadata> {
279        self.inner.close().await
280    }
281
282    async fn abort(&mut self) -> Result<()> {
283        self.inner.abort().await
284    }
285}