pub struct RateLimiter { /* private fields */ }Expand description
NCBI E-utilities rate limits:
- 3 requests per second without API key
- 10 requests per second with API key
- Violations can result in IP blocking
Token bucket rate limiter for NCBI API compliance
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(rate: f64) -> Self
pub fn new(rate: f64) -> Self
Create a new rate limiter with the specified rate
§Arguments
rate- Maximum requests per second (e.g., 3.0 for NCBI default)
§Examples
use pubmed_client::RateLimiter;
// Create rate limiter for NCBI API without key (3 req/sec)
let limiter_default = RateLimiter::new(3.0);
// Create rate limiter for NCBI API with key (10 req/sec)
let limiter_with_key = RateLimiter::new(10.0);Sourcepub fn ncbi_default() -> Self
pub fn ncbi_default() -> Self
Create rate limiter for NCBI API without API key (3 requests/second)
Sourcepub fn ncbi_with_key() -> Self
pub fn ncbi_with_key() -> Self
Create rate limiter for NCBI API with API key (10 requests/second)
Sourcepub async fn acquire(&self) -> Result<()>
pub async fn acquire(&self) -> Result<()>
Acquire a token, waiting if necessary to respect rate limits
This method implements a token bucket algorithm with the following behavior:
- Check if tokens are available in the bucket
- If available, consume one token and return immediately
- If not available, wait for the appropriate interval
- Refill the bucket and consume one token
§Examples
use pubmed_client::RateLimiter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let limiter = RateLimiter::ncbi_default();
// This will respect the 3 req/sec limit
for i in 0..5 {
limiter.acquire().await?;
println!("Making API call {}", i + 1);
// Make your API call here
}
Ok(())
}Sourcepub fn check_available(&self) -> bool
pub fn check_available(&self) -> bool
Check if a token is available without blocking
Returns true if a token is available and can be acquired immediately.
This method does not consume a token.
Sourcepub fn token_count(&self) -> f64
pub fn token_count(&self) -> f64
Get current token count (for testing and monitoring)
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
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 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
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 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>
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