-
-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
respect rate limits during pagination by sleeping #167
Changes from 5 commits
b75bfcb
8542d00
791e00f
e041d16
812e78f
1e44ccc
c8e11eb
549563b
f348110
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,23 +7,31 @@ class Cursor | |
|
||
attr_reader :client | ||
attr_reader :verb | ||
attr_reader :pause | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel a bit uncomfortable calling this |
||
attr_reader :params | ||
|
||
def initialize(client, verb, params = {}) | ||
@client = client | ||
@verb = verb | ||
@pause = params.delete(:pause) | ||
@params = params | ||
end | ||
|
||
def each | ||
next_cursor = nil | ||
loop do | ||
query = { limit: client.default_page_size }.merge(params).merge(cursor: next_cursor) | ||
response = client.send(verb, query) | ||
begin | ||
response = client.send(verb, query) | ||
rescue Slack::Web::Api::Errors::TooManyRequestsError => e | ||
sleep(e.retry_after.seconds) | ||
next | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think we should have a max retry count with some default set fairly high. If slack blacklists you somehow you'll have code that never ends and it will be impossible to debug. I also think there should be a log line here in DEBUG mode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think the count should reset after a successful request? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. |
||
end | ||
yield response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move this inside the rescue block and avoid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also did I tell you that I was a nitpicky code reviewer? Hang on tight :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand, I'm the same way 😎. Which part are you suggesting go in the rescue block? I started out with everything inside the begin block (see first commit b75bfcb) which avoids the I changed it (8542d00) because I thought it was bad practice to have more lines than necessary inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually now think yours is better. Someone can do something inside the yield that causes that exception and we're going to have bad side effects and catching an error we shouldn't. |
||
break unless response.response_metadata | ||
next_cursor = response.response_metadata.next_cursor | ||
break if next_cursor.blank? | ||
sleep(pause) if pause | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe simply "Added support for pausing between paginated requests that cause Slack rate limiting"?