pub struct Ratelimiter { /* private fields */ }
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
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 set_ratelimit_callback(
&mut self,
ratelimit_callback: Box<dyn Fn(RatelimitInfo) + Send + Sync>,
)
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.
pub fn set_absolute_ratelimits(&mut self, absolute_ratelimits: bool)
Sourcepub fn routes(
&self,
) -> Arc<RwLock<HashMap<RatelimitingBucket, Arc<Mutex<Ratelimit>>>>>
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);
}
}