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 config minimal wait duration #10

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion src/limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ struct Bucket<I> {
/// The number of bytes the bucket is carrying at the time `last_updated`.
/// This value can be negative.
value: f64,
/// The minimum duration to wait if self.value is smaller than 0 after
/// call `self.consume` (unit: s).
/// By default, is the same as `refill`.
min_wait: f64,
}

impl<I> Bucket<I> {
Expand All @@ -51,7 +55,7 @@ impl<I> Bucket<I> {
if self.value > 0.0 {
Duration::from_secs(0)
} else {
let sleep_secs = self.refill - self.value / self.speed_limit;
let sleep_secs = self.min_wait - self.value / self.speed_limit;
Duration::from_secs_f64(sleep_secs)
}
}
Expand Down Expand Up @@ -111,6 +115,7 @@ impl<I: Copy + Sub<Output = Duration>> Bucket<I> {
pub struct Builder<C: Clock> {
clock: C,
bucket: Bucket<C::Instant>,
min_wait: Option<f64>,
}

impl<C: Clock> Builder<C> {
Expand All @@ -125,6 +130,7 @@ impl<C: Clock> Builder<C> {
speed_limit: 0.0,
refill: 0.1,
value: 0.0,
min_wait: 0.1,
},
clock,
};
Expand Down Expand Up @@ -163,6 +169,12 @@ impl<C: Clock> Builder<C> {
self
}

/// Set the minimum wait duration.
kennytm marked this conversation as resolved.
Show resolved Hide resolved
pub fn min_wait(&mut self, dur: Duration) -> &mut Self {
self.min_wait = Some(dur.as_secs_f64());
self
}

/// Sets the clock instance used by the limiter.
pub fn clock(&mut self, clock: C) -> &mut Self {
self.clock = clock;
Expand All @@ -174,6 +186,8 @@ impl<C: Clock> Builder<C> {
self.bucket.value = self.bucket.capacity();
self.bucket.last_updated = self.clock.now();
let is_unlimited = self.bucket.speed_limit.is_infinite();
let min_wait = self.min_wait.unwrap_or(self.bucket.refill);
self.bucket.min_wait = min_wait;
Limiter {
bucket: Arc::new(Mutex::new(self.bucket)),
clock: mem::take(&mut self.clock),
Expand Down
Loading