Skip to main content

RateLimiter

Struct RateLimiter 

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

Rate limiter for controlling API request rates

Uses the governor crate to implement a token bucket algorithm for rate limiting API requests, with one independent bucket per RateLimitClass. See the module documentation for the enforced budget of each class.

Implementations§

Source§

impl RateLimiter

Source

pub fn new(config: &RateLimiterConfig) -> Self

Creates a new rate limiter from configuration

The non-trading bucket honors the configured budget exactly: it admits config.max_requests requests per config.period_seconds, with config.burst_size burst capacity (replenishing one cell every period_seconds / max_requests). The trading and historical buckets use stricter, fixed budgets derived from IG’s published limits and are independent of the configured budget. See the module documentation.

§Arguments
  • config - Rate limiter configuration containing max requests, period, and burst size
§Returns

A new RateLimiter instance

§Example
use ig_client::application::config::RateLimiterConfig;
use ig_client::application::rate_limiter::RateLimiter;

let config = RateLimiterConfig {
    max_requests: 60,
    period_seconds: 60,
    burst_size: 10,
};

// Non-trading admits 60 requests/minute (one cell per second, +burst).
let limiter = RateLimiter::new(&config);
Source

pub async fn wait_for(&self, class: RateLimitClass)

Waits until a request in the given class can be made according to its rate limit.

Uses governor’s async scheduler (until_ready): the future is parked until a slot is available rather than busy-polling. Each class has an independent bucket, so waiting on one class never blocks another.

§Example
use ig_client::application::rate_limiter::RateLimitClass;
limiter.wait_for(RateLimitClass::Trading).await;
// Place order here
Source

pub fn check_for(&self, class: RateLimitClass) -> bool

Checks if a request in the given class can be made immediately without waiting.

§Returns
  • true if a request can be made immediately
  • false if that class’s rate limit has been reached
Source

pub async fn wait(&self)

Waits until a non-trading request can be made according to the rate limit

Convenience wrapper over wait_for with RateLimitClass::NonTrading. Blocks (asynchronously) until the rate limiter allows the request to proceed.

§Example
limiter.wait().await;
// Make API request here
Source

pub fn check(&self) -> bool

Checks if a non-trading request can be made immediately without waiting

Convenience wrapper over check_for with RateLimitClass::NonTrading.

§Returns
  • true if a request can be made immediately
  • false if the rate limit has been reached
§Example
if limiter.check() {
    // Make API request
} else {
    // Wait or handle rate limit
}

Trait Implementations§

Source§

impl Clone for RateLimiter

Source§

fn clone(&self) -> RateLimiter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RateLimiter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more