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
122    fn info(&self) -> ServiceInfo {
123        self.inner.info()
124    }
125
126    fn capability(&self) -> Capability {
127        self.inner.capability()
128    }
129
130    async fn create_dir(
131        &self,
132        ctx: &OperationContext,
133        path: &str,
134        args: OpCreateDir,
135    ) -> Result<RpCreateDir> {
136        self.inner.create_dir(ctx, path, args).await
137    }
138
139    async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
140        self.inner.stat(ctx, path, args).await
141    }
142
143    fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
144        let limiter = self.rate_limiter.clone();
145
146        self.inner
147            .read(ctx, path, args)
148            .map(|r| ThrottleWrapper::new(r, limiter))
149    }
150
151    fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
152        let limiter = self.rate_limiter.clone();
153
154        self.inner
155            .write(ctx, path, args)
156            .map(|w| ThrottleWrapper::new(w, limiter))
157    }
158
159    fn copy(
160        &self,
161        ctx: &OperationContext,
162        from: &str,
163        to: &str,
164        args: OpCopy,
165        opts: OpCopier,
166    ) -> Result<Self::Copier> {
167        self.inner.copy(ctx, from, to, args, opts)
168    }
169
170    fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
171        self.inner.delete(ctx)
172    }
173
174    async fn rename(
175        &self,
176        ctx: &OperationContext,
177        from: &str,
178        to: &str,
179        args: OpRename,
180    ) -> Result<RpRename> {
181        self.inner.rename(ctx, from, to, args).await
182    }
183
184    fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
185        self.inner.list(ctx, path, args)
186    }
187
188    async fn presign(
189        &self,
190        ctx: &OperationContext,
191        path: &str,
192        args: OpPresign,
193    ) -> Result<RpPresign> {
194        self.inner.presign(ctx, path, args).await
195    }
196}
197
198#[doc(hidden)]
199pub struct ThrottleWrapper<R> {
200    inner: R,
201    limiter: SharedRateLimiter,
202}
203
204impl<R> ThrottleWrapper<R> {
205    fn new(inner: R, rate_limiter: SharedRateLimiter) -> Self {
206        Self {
207            inner,
208            limiter: rate_limiter,
209        }
210    }
211}
212
213impl<R: oio::ReadStream> oio::ReadStream for ThrottleWrapper<R> {
214    async fn read(&mut self) -> Result<Buffer> {
215        self.inner.read().await
216    }
217}
218
219impl<R: oio::Read> oio::Read for ThrottleWrapper<R> {
220    async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn oio::ReadStreamDyn>)> {
221        let (rp, stream) = self.inner.open(range).await?;
222        Ok((
223            rp,
224            Box::new(ThrottleWrapper::new(stream, self.limiter.clone()))
225                as Box<dyn oio::ReadStreamDyn>,
226        ))
227    }
228
229    async fn read(&self, range: BytesRange) -> Result<(RpRead, Buffer)> {
230        self.inner.read(range).await
231    }
232}
233
234impl<R: oio::Write> oio::Write for ThrottleWrapper<R> {
235    async fn write(&mut self, bs: Buffer) -> Result<()> {
236        let len = bs.len();
237        if len == 0 {
238            return self.inner.write(bs).await;
239        }
240
241        if len > u32::MAX as usize {
242            return Err(Error::new(
243                ErrorKind::RateLimited,
244                "request size exceeds throttle quota capacity",
245            ));
246        }
247
248        let buf_length =
249            NonZeroU32::new(len as u32).expect("len is non-zero so NonZeroU32 must exist");
250
251        self.limiter.until_n_ready(buf_length).await.map_err(|_| {
252            Error::new(
253                ErrorKind::RateLimited,
254                "burst size is smaller than the request size",
255            )
256        })?;
257
258        self.inner.write(bs).await
259    }
260
261    async fn close(&mut self) -> Result<Metadata> {
262        self.inner.close().await
263    }
264
265    async fn abort(&mut self) -> Result<()> {
266        self.inner.abort().await
267    }
268}