Skip to content

Commit af87e19

Browse files
committed
Add support for standard retry-after rate limiting.
1 parent 8b27035 commit af87e19

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/async/rest/wrapper/generic.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@ module Async
77
module REST
88
module Wrapper
99
class Generic
10+
protected def response_for(request)
11+
while true
12+
response = resource.call(request)
13+
14+
if response.status == 429
15+
if retry_after = response.headers['retry-after']
16+
sleep(retry_after.to_f)
17+
else
18+
# Without the `retry-after` header, we can't determine how long to wait, so we just return the response.
19+
return response
20+
end
21+
else
22+
return response
23+
end
24+
end
25+
end
26+
1027
def call(resource, method = "GET", payload = nil, &block)
1128
request = ::Protocol::HTTP::Request[method, nil]
1229

1330
self.prepare_request(request, payload)
1431

15-
response = resource.call(request)
32+
response = self.response_for(request)
1633

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

0 commit comments

Comments
 (0)