pub struct RateLimiter { /* private fields */ }Expand description
In-memory rate limiter that manages token buckets for different endpoints Thread-safe but not cross-process
Implementations§
Source§impl RateLimiter
impl RateLimiter
pub fn new() -> Self
Sourcepub async fn wait_for_token(
&self,
identifier: &str,
rate: f64,
burst: u32,
) -> Result<()>
pub async fn wait_for_token( &self, identifier: &str, rate: f64, burst: u32, ) -> Result<()>
Wait for a token to become available for the given endpoint This method will block until a token is available
Examples found in repository?
examples/sellers_v1_use_raw_api.rs (line 12)
6async fn main() -> Result<()> {
7 let spapi_config = SpapiConfig::from_env()?;
8 let client = SpapiClient::new(spapi_config.clone())?;
9 let configuration = client.create_configuration().await?;
10 client
11 .limiter()
12 .wait_for_token("get_marketplace_participations", 0.016, 15)
13 .await?;
14 let res = get_marketplace_participations(&configuration).await;
15 client.limiter()
16 .record_response("get_marketplace_participations")
17 .await?;
18 println!("Marketplace Participations: {:#?}", res);
19 Ok(())
20}Sourcepub async fn check_token_availability(&self, identifier: &str) -> Result<bool>
pub async fn check_token_availability(&self, identifier: &str) -> Result<bool>
Check if a token is available without consuming it
Sourcepub async fn get_token_status(&self) -> Result<HashMap<String, (f64, f64, u32)>>
pub async fn get_token_status(&self) -> Result<HashMap<String, (f64, f64, u32)>>
Get current token status for all endpoints Returns (tokens, rate, burst) for each endpoint
Sourcepub async fn active_buckets_count(&self) -> usize
pub async fn active_buckets_count(&self) -> usize
Get the number of active buckets
Sourcepub async fn record_response(&self, identifier: &str) -> Result<()>
pub async fn record_response(&self, identifier: &str) -> Result<()>
Record that a response was received for the given endpoint This updates the last_response_time used for enforcing minimum intervals
Examples found in repository?
examples/sellers_v1_use_raw_api.rs (line 16)
6async fn main() -> Result<()> {
7 let spapi_config = SpapiConfig::from_env()?;
8 let client = SpapiClient::new(spapi_config.clone())?;
9 let configuration = client.create_configuration().await?;
10 client
11 .limiter()
12 .wait_for_token("get_marketplace_participations", 0.016, 15)
13 .await?;
14 let res = get_marketplace_participations(&configuration).await;
15 client.limiter()
16 .record_response("get_marketplace_participations")
17 .await?;
18 println!("Marketplace Participations: {:#?}", res);
19 Ok(())
20}Trait Implementations§
Auto Trait Implementations§
impl Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin 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
Mutably borrows from an owned value. Read more