pub struct RateLimiter { /* private fields */ }Expand description
Discord HTTP client API rate limiter.
The RateLimiter runs an associated actor task to concurrently handle permit
requests and responses.
Cloning a RateLimiter increments just the amount of senders for the actor.
The actor completes when there are no senders and non-completed permits left.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(global_limit: u16) -> Self
pub fn new(global_limit: u16) -> Self
Create a new RateLimiter with a custom global limit.
Sourcepub fn acquire(&self, endpoint: Endpoint) -> PermitFuture ⓘ
pub fn acquire(&self, endpoint: Endpoint) -> PermitFuture ⓘ
Await a single permit for this endpoint.
Permits are queued per endpoint in the order they were requested.
Sourcepub fn acquire_if<P>(
&self,
endpoint: Endpoint,
predicate: P,
) -> MaybePermitFuture ⓘ
pub fn acquire_if<P>( &self, endpoint: Endpoint, predicate: P, ) -> MaybePermitFuture ⓘ
Await a single permit for this endpoint, but only if the predicate evaluates
to true.
Permits are queued per endpoint in the order they were requested.
Note that the predicate is asynchronously called in the actor task.
§Example
use twilight_http_ratelimiting::{Endpoint, Method};
let endpoint = Endpoint {
method: Method::Get,
path: String::from("applications/@me"),
};
if let Some(permit) = rate_limiter
.acquire_if(endpoint, |b| b.is_none_or(|b| b.remaining > 10))
.await
{
let headers = unimplemented!("GET /applications/@me");
permit.complete(headers);
}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 moreSource§impl Debug for RateLimiter
impl Debug for RateLimiter
Source§impl Default for RateLimiter
impl Default for RateLimiter
Source§fn default() -> Self
fn default() -> Self
Create a new RateLimiter with Discord’s default global limit.
Currently this is 50.
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