Struct serenity::http::Ratelimiter

source ·
pub struct Ratelimiter { /* private fields */ }
Available on crate feature 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§

source§

impl Ratelimiter

source

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.

source

pub fn set_ratelimit_callback( &mut self, ratelimit_callback: Box<dyn Fn(RatelimitInfo) + Send + Sync> )

Sets a callback to be called when a route is rate limited.

source

pub fn set_absolute_ratelimits(&mut self, absolute_ratelimits: bool)

source

pub fn routes( &self ) -> Arc<RwLock<HashMap<RatelimitingBucket, Arc<Mutex<Ratelimit>>>>>

The routes mutex is a HashMap of each RatelimitingBucket 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::Route;

let routes = http.ratelimiter.unwrap().routes();
let reader = routes.read().await;

let channel_id = ChannelId::new(7);
let route = Route::Channel {
    channel_id,
};
if let Some(route) = reader.get(&route.ratelimiting_bucket()) {
    if let Some(reset) = route.lock().await.reset() {
        println!("Reset time at: {:?}", reset);
    }
}
source

pub async fn perform(&self, req: Request<'_>) -> Result<Response>

§Errors

Only error kind that may be returned is Error::Http.

Trait Implementations§

source§

impl Debug for Ratelimiter

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,