Struct leaky_bucket::Builder
source · pub struct Builder { /* private fields */ }
Expand description
A builder for a RateLimiter
.
Implementations§
source§impl Builder
impl Builder
sourcepub fn max(&mut self, max: usize) -> &mut Self
pub fn max(&mut self, max: usize) -> &mut Self
Configure the max number of tokens to use.
If unspecified, this will default to be 10 times the refill
or the
initial
value, whichever is largest.
The maximum supported balance is limited to isize::MAX
.
§Examples
use leaky_bucket::RateLimiter;
let limiter = RateLimiter::builder()
.max(10_000)
.build();
sourcepub fn initial(&mut self, initial: usize) -> &mut Self
pub fn initial(&mut self, initial: usize) -> &mut Self
Configure the initial number of tokens to configure. The default value
is 0
.
§Examples
use leaky_bucket::RateLimiter;
let limiter = RateLimiter::builder()
.initial(10)
.build();
sourcepub fn interval(&mut self, interval: Duration) -> &mut Self
pub fn interval(&mut self, interval: Duration) -> &mut Self
Configure the time duration between which we add refill
number to
the bucket rate limiter.
This is 100ms by default.
§Panics
This panics if the provided interval does not fit within the millisecond bounds of a usize or is zero.
use leaky_bucket::RateLimiter;
use tokio::time::Duration;
let limiter = RateLimiter::builder()
.interval(Duration::from_secs(u64::MAX))
.build();
use leaky_bucket::RateLimiter;
use tokio::time::Duration;
let limiter = RateLimiter::builder()
.interval(Duration::from_millis(0))
.build();
§Examples
use leaky_bucket::RateLimiter;
use tokio::time::Duration;
let limiter = RateLimiter::builder()
.interval(Duration::from_millis(100))
.build();
sourcepub fn fair(&mut self, fair: bool) -> &mut Self
pub fn fair(&mut self, fair: bool) -> &mut Self
Configure the rate limiter to be fair.
Fairness is enabled by deafult.
Fairness ensures that tasks make progress in the order that they acquire even when the rate limiter is under contention. An unfair scheduler might have a higher total throughput.
Fair scheduling also affects the behavior of
RateLimiter::try_acquire
which will return false
if there are any
pending tasks since they should be given priority.
§Examples
use leaky_bucket::RateLimiter;
let limiter = RateLimiter::builder()
.refill(100)
.fair(false)
.build();
sourcepub fn build(&self) -> RateLimiter
pub fn build(&self) -> RateLimiter
Construct a new RateLimiter
.
§Examples
use leaky_bucket::RateLimiter;
use tokio::time::Duration;
let limiter = RateLimiter::builder()
.refill(100)
.interval(Duration::from_millis(200))
.max(10_000)
.build();