Skip to content

Commit

Permalink
Add support for standard retry-after rate limiting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 26, 2024
1 parent 8b27035 commit af87e19
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/async/rest/wrapper/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ module Async
module REST
module Wrapper
class Generic
protected def response_for(request)
while true
response = resource.call(request)

if response.status == 429
if retry_after = response.headers['retry-after']
sleep(retry_after.to_f)
else
# Without the `retry-after` header, we can't determine how long to wait, so we just return the response.
return response
end
else
return response
end
end
end

def call(resource, method = "GET", payload = nil, &block)
request = ::Protocol::HTTP::Request[method, nil]

self.prepare_request(request, payload)

response = resource.call(request)
response = self.response_for(request)

# If we exit this block because of an exception, we close the response. This ensures we don't have any dangling connections.
begin
Expand Down

0 comments on commit af87e19

Please sign in to comment.