Skip to main content

RateLimitBuilder

Struct RateLimitBuilder 

Source
pub struct RateLimitBuilder { /* private fields */ }
Expand description

Builder pattern implementation for creating rate limit instances.

This struct provides a convenient way to configure and create rate limit instances with various options such as clustering, custom buckets, and distributed storage. It extracts necessary dependencies from the configuration context and maintains shared references to components like clocks, storage, and locks.

Implementations§

Source§

impl RateLimitBuilder

Source

pub fn new(&self, builder_id: String) -> RateLimitBuilderInstance

Creates a new builder instance for configuring a rate limiter.

§Arguments
  • builder_id - A string identifier for the rate limit instance.
§Returns

A new RateLimitBuilderInstance configured in local mode by default. It allows for further configuration before building the final RateLimitInstance. The returned instance inherits all the configuration and dependencies from this builder.

Additional methods provided:

  • buckets: Allows custom quotas configuration as per-policy, per-SLA, or per-user grouping.
  • clustered: Enables distributed, cross-node rate limiting by invoking .clustered(timer) before .build(). This requires a distributed storage client and configures the limiter for quota sharing across a cluster.
  • shared: Enables sharing the rate limit across different policy instances
  • build: Creates a new RateLimitInstance.
§Example
// Local mode (single node, in-memory):
let local_limiter = builder
    .new("unique-id".to_string())
    .build()?;

// Cluster mode (multi-node, distributed quota):
let custom_buckets = vec![
    (
        "default".to_string(),
        vec![
            Tier {
                period_in_millis: 60000,
                requests: 120,
            },
        ],
    ),
];

let timer = clock.period(Duration::from_secs(5));

let cluster_limiter = builder
    .new("unique-id".to_string())
    .buckets(custom_buckets)
    .clustered(timer)
    .build()?;

// Shared mode (across policy instances):
let shared_limiter = builder
    .new("unique-id".to_string())
    .shared()
    .build()?;

Trait Implementations§

Auto Trait Implementations§

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<C, T> Extract<T> for C
where T: FromContext<C>,

Source§

type Error = <T as FromContext<C>>::Error

Source§

fn extract(&self) -> Result<T, <C as Extract<T>>::Error>

Source§

fn extract_always(&self) -> T
where Self: Extract<T, Error = Infallible>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<C, T> FromContextOnce<C, Repeat> for T
where T: for<'c> FromContext<C> + 'c,

Source§

type Error = <T as FromContext<C>>::Error

Source§

type Future<'c> = Ready<Result<T, <T as FromContextOnce<C, Repeat>>::Error>> where C: 'c, T: 'c

Source§

fn from_context_once( context: Exclusive<'_, C>, ) -> <T as FromContextOnce<C, Repeat>>::Future<'_>

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> Same for T

Source§

type Output = T

Should always be Self
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.