Skip to content

Commit

Permalink
feat(reqwest-rate-limiter): add default rate limiter middleware; bump…
Browse files Browse the repository at this point in the history
… dependencies (#979)

* bump `governor` to v0.6
* bump `reqwest` to v0.12
* bump `reqwest-middleware` to v0.3

* feat: add DefaultRateLimiterMiddleware
  • Loading branch information
rimrakhimov authored Jul 22, 2024
1 parent acf11c8 commit f7dcb38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
10 changes: 5 additions & 5 deletions libs/reqwest-rate-limiter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.68"
governor = "0.5.1"
reqwest = "0.11.18"
reqwest-middleware = "0.2.2"
task-local-extensions = "0.1.4"
async-trait = "0.1"
governor = "0.6"
reqwest = "0.12"
reqwest-middleware = "0.3"
http = "1.1.0"
57 changes: 39 additions & 18 deletions libs/reqwest-rate-limiter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
use governor::{
clock::{Clock, ReasonablyRealtime},
middleware::RateLimitingMiddleware,
state::{NotKeyed, StateStore},
NotUntil, RateLimiter,
};
use governor::{clock, middleware, state, NotUntil, RateLimiter};
use reqwest::{Request, Response};
use reqwest_middleware::{Middleware, Next};
use std::sync::Arc;
use std::{num::NonZeroU32, sync::Arc};

pub type DefaultRateLimiterMiddleware<
MW = middleware::NoOpMiddleware<<clock::DefaultClock as clock::Clock>::Instant>,
> = RateLimiterMiddleware<state::direct::NotKeyed, state::InMemoryState, clock::DefaultClock, MW>;

impl DefaultRateLimiterMiddleware {
/// Default rate limiter splits the given period evenly between quotas
/// and allows only 1 request per the cell.
const BURST_SIZE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(1) };

pub fn per_second(max_burst: NonZeroU32) -> Self {
let quota = governor::Quota::per_second(max_burst).allow_burst(Self::BURST_SIZE);
RateLimiterMiddleware::new(RateLimiter::direct(quota))
}

pub fn per_minute(max_burst: NonZeroU32) -> Self {
let quota = governor::Quota::per_minute(max_burst).allow_burst(Self::BURST_SIZE);
RateLimiterMiddleware::new(RateLimiter::direct(quota))
}

pub fn per_hour(max_burst: NonZeroU32) -> Self {
let quota = governor::Quota::per_hour(max_burst).allow_burst(Self::BURST_SIZE);
RateLimiterMiddleware::new(RateLimiter::direct(quota))
}
}

#[derive(Clone)]
pub struct RateLimiterMiddleware<K, S, C, MW>
where
S: StateStore<Key = K>,
C: Clock,
MW: RateLimitingMiddleware<C::Instant>,
S: state::StateStore<Key = K>,
C: clock::Clock,
MW: middleware::RateLimitingMiddleware<C::Instant>,
{
rate_limiter: Arc<RateLimiter<K, S, C, MW>>,
}

impl<K, S, C, MW> RateLimiterMiddleware<K, S, C, MW>
where
S: StateStore<Key = K>,
C: Clock,
MW: RateLimitingMiddleware<C::Instant>,
S: state::StateStore<Key = K>,
C: clock::Clock,
MW: middleware::RateLimitingMiddleware<C::Instant>,
{
pub fn new(rate_limiter: RateLimiter<K, S, C, MW>) -> Self {
Self::new_arc(Arc::new(rate_limiter))
Expand All @@ -33,11 +54,11 @@ where
}

#[async_trait::async_trait]
impl<S, C, MW, PO> Middleware for RateLimiterMiddleware<NotKeyed, S, C, MW>
impl<S, C, MW, PO> Middleware for RateLimiterMiddleware<state::direct::NotKeyed, S, C, MW>
where
S: StateStore<Key = NotKeyed> + Send + Sync + 'static,
C: Clock + ReasonablyRealtime + Send + Sync + 'static,
MW: RateLimitingMiddleware<
S: state::StateStore<Key = state::direct::NotKeyed> + Send + Sync + 'static,
C: clock::Clock + clock::ReasonablyRealtime + Send + Sync + 'static,
MW: middleware::RateLimitingMiddleware<
C::Instant,
NegativeOutcome = NotUntil<C::Instant>,
PositiveOutcome = PO,
Expand All @@ -49,7 +70,7 @@ where
async fn handle(
&self,
req: Request,
extensions: &mut task_local_extensions::Extensions,
extensions: &mut http::Extensions,
next: Next<'_>,
) -> reqwest_middleware::Result<Response> {
self.rate_limiter.until_ready().await;
Expand Down

0 comments on commit f7dcb38

Please sign in to comment.