javadiscord / java-discord-api

A wrapper over the discord API to create bots using Java
GNU General Public License v3.0
7 stars 8 forks source link

Handle rate limiting based on the Headers provided by Discord #110

Closed surajkumar closed 1 month ago

surajkumar commented 1 month ago

As a user I want to...

be able to send requests to discord at the rate they allow

So that...

I am not rate limited or be imposed by discord.

Context

At the moment we are handling rate limiting by not allowing more than 50 requests per second. This is actually incorrect according to the discord documentation: https://discord.com/developers/docs/topics/rate-limits#rate-limits

Headers we receive:

x-ratelimit-bucket: [b1efb5d172880458c9f9f65482781f7e]
x-ratelimit-limit: [5]
x-ratelimit-remaining: [4]
x-ratelimit-reset: [1716473972]
x-ratelimit-reset-after: [3]

From the docs:

X-RateLimit-Limit - The number of requests that can be made
X-RateLimit-Remaining - The number of remaining requests that can be made
X-RateLimit-Reset - Epoch time (seconds since 00:00:00 UTC on January 1, 1970) at which the rate limit resets
X-RateLimit-Reset-After - Total time (in seconds) of when the current rate limit bucket will reset. Can have decimals to match previous millisecond ratelimit precision
X-RateLimit-Bucket - A unique string denoting the rate limit being encountered (non-inclusive of top-level resources in the path)
X-RateLimit-Global - Returned only on HTTP 429 responses if the rate limit encountered is the global rate limit (not per-route)
X-RateLimit-Scope - Returned only on HTTP 429 responses. Value can be user (per bot or user limit), global (per bot or user global limit), or shared (per resource limit)

Acceptance Criteria

WHEN make a request to discord THEN we should read the headers to handle rate limiting for the next request