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
impl RateLimiter
Sourcepub fn new(config: &RateLimiterConfig) -> Self
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);Sourcepub async fn wait_for(&self, class: RateLimitClass)
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 hereSourcepub fn check_for(&self, class: RateLimitClass) -> bool
pub fn check_for(&self, class: RateLimitClass) -> bool
Checks if a request in the given class can be made immediately without waiting.
§Returns
trueif a request can be made immediatelyfalseif that class’s rate limit has been reached
Sourcepub fn check(&self) -> bool
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
trueif a request can be made immediatelyfalseif 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
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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