pub struct RateLimiter { /* private fields */ }Expand description
Token-bucket rate limiter for per-domain and global request throttling.
Each domain gets its own token bucket. A global bucket throttles all requests across all domains. A request proceeds only when both the domain and global buckets have tokens. Supports crawl-delay from robots.txt and optional concurrency limiting.
§Examples
use crawlkit_engine::ratelimit::RateLimiter;
let limiter = RateLimiter::new(2.0, 10.0);
assert!((limiter.per_domain_rps() - 2.0).abs() < f64::EPSILON);Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(per_domain_rps: f64, global_rps: f64) -> Self
pub fn new(per_domain_rps: f64, global_rps: f64) -> Self
Creates a new rate limiter with the given RPS settings.
Sourcepub fn from_crawl_config(config: &CrawlConfig) -> Self
pub fn from_crawl_config(config: &CrawlConfig) -> Self
Creates a rate limiter from a CrawlConfig.
Sourcepub fn with_concurrency(self, max_concurrent: usize) -> Self
pub fn with_concurrency(self, max_concurrent: usize) -> Self
Creates a rate limiter with a concurrency limit.
Sourcepub async fn acquire(&self, domain: &str) -> Result<(), RateLimitError>
pub async fn acquire(&self, domain: &str) -> Result<(), RateLimitError>
Acquires permission to make a request to the given domain.
Blocks until tokens are available in both the domain and global
buckets. Returns Err if the wait exceeds the timeout.
Sourcepub async fn acquire_with_timeout(
&self,
domain: &str,
timeout: Duration,
) -> Result<(), RateLimitError>
pub async fn acquire_with_timeout( &self, domain: &str, timeout: Duration, ) -> Result<(), RateLimitError>
Acquires permission with a timeout.
Sourcepub fn set_crawl_delay(&self, domain: &str, delay: Duration)
pub fn set_crawl_delay(&self, domain: &str, delay: Duration)
Sets the crawl-delay for a specific domain (from robots.txt).
This overrides the default RPS for that domain.
Sourcepub fn domain_tokens(&self, domain: &str) -> f64
pub fn domain_tokens(&self, domain: &str) -> f64
Returns the current token count for a domain bucket. Creates the bucket if it doesn’t exist.
Sourcepub fn global_tokens(&self) -> f64
pub fn global_tokens(&self) -> f64
Returns the current global token count.
Sourcepub fn per_domain_rps(&self) -> f64
pub fn per_domain_rps(&self) -> f64
Returns the per-domain RPS setting.
Sourcepub fn global_rps(&self) -> f64
pub fn global_rps(&self) -> f64
Returns the global RPS setting.
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin 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> 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