Skip to main content

Builder

Struct Builder 

Source
pub struct Builder<C: Clock + Clone = SystemClock> { /* private fields */ }
Expand description

A fluent builder for a RateLimiter when the Tier-1 constructors are not enough.

Start it with RateLimiter::builder, chain the knobs you care about — algorithm, quota, burst, shard count, eviction policy, clock — and call build. Anything left unset keeps a sane default: the token bucket, default sharding, and the bounded-memory Eviction default. The quota defaults to a limit of 0 (which denies everything), so set it.

build is infallible: a zero limit produces a deny-everything limiter, and a zero period a degenerate one, rather than an error.

§Examples

use rate_net::{RateLimiter, Eviction};
use std::time::Duration;

let limiter = RateLimiter::builder()
    .quota(1000, Duration::from_secs(60)) // 1000 / minute
    .burst(50)                            // allow short bursts of 50
    .shards(64)                           // tune for core count
    .eviction(Eviction::idle(Duration::from_secs(300)))
    .build();

assert_eq!(limiter.quota().limit(), 1000);
assert_eq!(limiter.quota().burst(), 50);
assert_eq!(limiter.shards(), 64);

Implementations§

Source§

impl<C: Clock + Clone> Builder<C>

Source

pub fn algorithm(self, algorithm: Algorithm) -> Self

Selects the algorithm. Defaults to Algorithm::TokenBucket. The leaky bucket and window algorithms require the algorithms feature.

§Examples
use rate_net::{RateLimiter, Algorithm};
use std::time::Duration;

let limiter = RateLimiter::builder()
    .algorithm(Algorithm::SlidingWindowCounter)
    .quota(100, Duration::from_secs(1))
    .build();
assert_eq!(limiter.algorithm(), Algorithm::SlidingWindowCounter);
Source

pub fn quota(self, limit: u32, period: Duration) -> Self

Sets the quota: limit requests per period, per key.

§Examples
use rate_net::RateLimiter;
use std::time::Duration;

let limiter = RateLimiter::builder().quota(5, Duration::from_millis(100)).build();
assert_eq!(limiter.quota().limit(), 5);
Source

pub fn per_second(self, limit: u32) -> Self

Sets the quota to limit requests per second.

§Examples
use rate_net::RateLimiter;

let limiter = RateLimiter::builder().per_second(100).build();
assert_eq!(limiter.quota().limit(), 100);
Source

pub fn per_minute(self, limit: u32) -> Self

Sets the quota to limit requests per minute.

§Examples
use rate_net::RateLimiter;
use std::time::Duration;

let limiter = RateLimiter::builder().per_minute(600).build();
assert_eq!(limiter.quota().period(), Duration::from_secs(60));
Source

pub fn burst(self, burst: u32) -> Self

Sets the burst ceiling. Defaults to the quota’s limit. Applies to the token and leaky buckets; window algorithms ignore it.

§Examples
use rate_net::RateLimiter;

let limiter = RateLimiter::builder().per_second(100).burst(250).build();
assert_eq!(limiter.quota().burst(), 250);
Source

pub fn shards(self, shards: usize) -> Self

Sets the shard count (rounded up to a power of two). Defaults to a small multiple of the core count.

§Examples
use rate_net::RateLimiter;

let limiter = RateLimiter::builder().per_second(1).shards(128).build();
assert_eq!(limiter.shards(), 128);
Source

pub fn eviction(self, eviction: Eviction) -> Self

Sets the eviction policy. Defaults to a bounded-memory capacity cap.

§Examples
use rate_net::{RateLimiter, Eviction};

let limiter = RateLimiter::builder().per_second(1).eviction(Eviction::capacity(1_000)).build();
assert_eq!(limiter.eviction().max_keys(), Some(1_000));
Source

pub fn clock<C2: Clock + Clone>(self, clock: C2) -> Builder<C2>

Sets the time source, for deterministic tests with a ManualClock.

§Examples
use rate_net::RateLimiter;
use clock_lib::ManualClock;
use std::sync::Arc;

let clock = Arc::new(ManualClock::new());
let limiter = RateLimiter::builder().per_second(5).clock(Arc::clone(&clock)).build();
assert!(limiter.check("k").is_allow());
Source

pub fn build(self) -> RateLimiter<C>

Builds the configured limiter.

§Examples
use rate_net::RateLimiter;
use std::time::Duration;

let limiter = RateLimiter::builder().quota(10, Duration::from_secs(1)).build();
assert!(limiter.check("k").is_allow());

Auto Trait Implementations§

§

impl<C> Freeze for Builder<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Builder<C>
where C: RefUnwindSafe,

§

impl<C> Send for Builder<C>

§

impl<C> Sync for Builder<C>

§

impl<C> Unpin for Builder<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Builder<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Builder<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,