Skip to content

Commit

Permalink
Fix: Fix rate limit handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi authored Apr 29, 2024
1 parent 173cf9b commit b967129
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions backend/lib/discord_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ def request(method, path, **options)
HTTP.request(method, "https://discord.com/api/v10#{path}", **options)

if response.headers["X-RateLimit-Remaining"]
@@ratelimits[response.headers["X-RateLimit-Bucket"]] = {
@@ratelimits[path] = {
remaining: response.headers["X-RateLimit-Remaining"].to_i,
reset: response.headers["X-RateLimit-Reset"].to_f
}
end
if response.status == 429
sleep(response.headers["Retry-After"].to_i / 1000.0)
request(method, path, **options)
retry_after = response.parse["retry_after"]
Rails.logger.warn("Rate limited, waiting for #{retry_after}s")
sleep(retry_after)
return request(method, path, **options)
end

unless response.status.success?
Expand Down

0 comments on commit b967129

Please sign in to comment.