Skip to content

Commit 74e9c43

Browse files
Merge pull request #409 from intercom/ak/handle_rate_limit_retries
Ak/handle rate limit retries
2 parents 5519a12 + 4a91065 commit 74e9c43

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ intercom.rate_limit_details
469469
```
470470

471471
You can handle the rate limits yourself but a simple option is to use the handle_rate_limit flag.
472-
This will automatically catch the 429 rate limit exceeded error and wait until the reset time to retry.
472+
This will automatically catch the 429 rate limit exceeded error and wait until the reset time to retry. After three retries a rate limit exception will be raised. Encountering this error frequently may require a revisiting of your usage of the API.
473473

474474
```
475475
intercom = Intercom::Client.new(token: ENV['AT'], handle_rate_limit: true)

lib/intercom/request.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ def execute(target_base_url=nil, username:, secret: nil, read_timeout: 90, open_
7575
rescue Intercom::RateLimitExceeded => e
7676
if @handle_rate_limit
7777
seconds_to_retry = (@rate_limit_details[:reset_at] - Time.now.utc).ceil
78-
sleep seconds_to_retry unless seconds_to_retry < 0
79-
retry unless (retries -=1).zero?
78+
if (retries -= 1) < 0
79+
raise Intercom::RateLimitExceeded.new('Rate limit retries exceeded. Please examine current API Usage.')
80+
else
81+
sleep seconds_to_retry unless seconds_to_retry < 0
82+
retry
83+
end
8084
else
8185
raise e
8286
end

spec/unit/intercom/request_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
client.handle_rate_limit.must_equal(true)
3131
end
3232

33-
it 'should call sleep for rate limit error three times' do
34-
# Use webmock to mock the HTTP request
35-
stub_request(:any, uri).\
36-
to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s })
37-
req = Intercom::Request.get(uri, "")
38-
req.handle_rate_limit=true
39-
req.expects(:sleep).times(3).with(any_parameters)
40-
req.execute(target_base_url=uri, username: "ted", secret: "")
33+
it 'should call sleep for rate limit error three times and raise a rate limit error otherwise' do
34+
expect {
35+
stub_request(:any, uri).\
36+
to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s })
37+
req = Intercom::Request.get(uri, "")
38+
req.handle_rate_limit=true
39+
req.expects(:sleep).times(3).with(any_parameters)
40+
req.execute(target_base_url=uri, username: "ted", secret: "")
41+
}.must_raise(Intercom::RateLimitExceeded)
4142
end
4243

4344
it 'should not call sleep for rate limit error' do

0 commit comments

Comments
 (0)