Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support huge (>1e9 element/sec) quotas #207

Merged
merged 5 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
secrets: "inherit"
uses: "boinkor-net/ci-baseline-rust/.github/workflows/ci_baseline_rust_tests.yml@main"
strategy:
fail-fast: false
matrix:
rust_toolchain: [nightly, stable]
cargo_test_args:
Expand Down
11 changes: 11 additions & 0 deletions governor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

## [Unreleased] - ReleaseDate

### Changed

* The `.per_second` constructor for `Quota` now constructs a quota
that ensures all rate-limiting calls succeed when given values in
excess of 1 billion (previously, this would result in rate limiters
that would incorrectly reject values). Reported in
[#203](https://github.com/antifuchs/governor/issues/203).

### Contributors
* [@rkd-msw](https://github.com/rkd-msw)

## [[0.6.0](https://docs.rs/governor/0.6.0/governor/)] - 2023-07-12

### Added
Expand Down
4 changes: 3 additions & 1 deletion governor/src/gcra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ pub(crate) struct Gcra {

impl Gcra {
pub(crate) fn new(quota: Quota) -> Self {
let tau: Nanos = (quota.replenish_1_per * quota.max_burst.get()).into();
let tau: Nanos = (cmp::max(quota.replenish_1_per, Duration::from_nanos(1))
* quota.max_burst.get())
.into();
let t: Nanos = quota.replenish_1_per.into();
Gcra { t, tau }
}
Expand Down
24 changes: 24 additions & 0 deletions governor/tests/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,27 @@ fn default_direct() {
RateLimiter::direct_with_clock(Quota::per_second(nonzero!(20u32)), &clock);
assert_eq!(Ok(()), limiter.check());
}

#[cfg(feature = "std")]
#[test]
fn stresstest_large_quotas() {
use std::{sync::Arc, thread};

use governor::middleware::StateInformationMiddleware;

let quota = Quota::per_second(nonzero!(1_000_000_001u32));
let rate_limiter =
Arc::new(RateLimiter::direct(quota).with_middleware::<StateInformationMiddleware>());

fn rlspin(rl: Arc<DefaultDirectRateLimiter<StateInformationMiddleware>>) {
for _ in 0..1_000_000 {
rl.check().map_err(|e| dbg!(e)).unwrap();
}
}

let rate_limiter2 = rate_limiter.clone();
thread::spawn(move || {
rlspin(rate_limiter2);
});
rlspin(rate_limiter);
}