Struct serenity::http::ratelimiting::Ratelimiter
source · [−]pub struct Ratelimiter { /* private fields */ }
http
only.Expand description
Ratelimiter for requests to the Discord API.
This keeps track of ratelimit data for known routes through the
Ratelimit
implementation for each route: how many tickets are
remaining
until the user needs to wait for the known reset
time, and
the limit
of requests that can be made within that time.
When no tickets are available for some time, then the thread sleeps until that time passes. The mechanism is known as “pre-emptive ratelimiting”.
Occasionally for very high traffic bots, a global ratelimit may be reached which blocks all future requests until the global ratelimit is over, regardless of route. The value of this global ratelimit is never given through the API, so it can’t be pre-emptively ratelimited. This only affects the largest of bots.
Implementations
sourceimpl Ratelimiter
impl Ratelimiter
sourcepub fn new(client: Client, token: impl Into<String>) -> Self
pub fn new(client: Client, token: impl Into<String>) -> Self
Creates a new ratelimiter, with a shared reqwest
client and the
bot’s token.
The bot token must be prefixed with "Bot "
. The ratelimiter does not
prefix it.
sourcepub fn routes(&self) -> Arc<RwLock<HashMap<Route, Arc<Mutex<Ratelimit>>>>>
pub fn routes(&self) -> Arc<RwLock<HashMap<Route, Arc<Mutex<Ratelimit>>>>>
The routes mutex is a HashMap of each Route
and their respective
ratelimit information.
See the documentation for Ratelimit
for more information on how the
library handles ratelimiting.
Examples
View the reset
time of the route for ChannelsId(7)
:
use serenity::http::ratelimiting::Route;
let routes = http.ratelimiter.routes();
let reader = routes.read().await;
if let Some(route) = reader.get(&Route::ChannelsId(7)) {
if let Some(reset) = route.lock().await.reset() {
println!("Reset time at: {:?}", reset);
}
}
sourcepub async fn perform(&self, req: RatelimitedRequest<'_>) -> Result<Response>
pub async fn perform(&self, req: RatelimitedRequest<'_>) -> Result<Response>
Errors
Only error kind that may be returned is Error::Http
.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Ratelimiter
impl Send for Ratelimiter
impl Sync for Ratelimiter
impl Unpin for Ratelimiter
impl !UnwindSafe for Ratelimiter
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more