From b967129f879f41d99e54364ea857abac16d64623 Mon Sep 17 00:00:00 2001 From: Nanashi Date: Tue, 30 Apr 2024 06:15:42 +0900 Subject: [PATCH] Fix: Fix rate limit handling --- backend/lib/discord_request.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/lib/discord_request.rb b/backend/lib/discord_request.rb index 0df0936..c6cccc2 100644 --- a/backend/lib/discord_request.rb +++ b/backend/lib/discord_request.rb @@ -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?