Module burger::rate_limit
source · Expand description
The ServiceExt::rate_limit
combinator returns RateLimit
,
which limits the number of Service::call
s invoked per period of time.
This implementation requires the number of permits and the interval is specified, each
Service::acquire
acquires a permit, when Service::call
is invoked the permit is
forgotten. The number of available permits is refreshed when the period has elapsed.
Note that this does not garauntee that a remote server will receive requests under these restrictions. Network conditions, other middleware, etc can cause requests to arrive in bursts exceeding the rate limit specified here.
§Example
If 5 permits and a interval of 2 second is specified then the first 5 Service::acquire
s will
immediately resolve and the 6th will resolve after the 2 second interval has elapsed.
use std::time::Duration;
use burger::*;
let svc =
service_fn(|x: u32| async move { x.to_string() }).rate_limit(Duration::from_secs(1), 5);
let response = svc.oneshot(1).await;
Structs§
- A wrapper for the
ServiceExt::rate_limit
combinator. - The
Service::Permit
type forRateLimit
.