Skip to content

Add TooManyRequests Exception#112

Merged
bboe merged 2 commits into
praw-dev:masterfrom
MaybeNetwork:too-many-requests
Jun 5, 2021
Merged

Add TooManyRequests Exception#112
bboe merged 2 commits into
praw-dev:masterfrom
MaybeNetwork:too-many-requests

Conversation

@MaybeNetwork
Copy link
Copy Markdown
Contributor

Feature Summary and Justification

This adds a TooManyRequests exception.

In PRAW's issue 1716, 429s are being returned because Reddit refuses to serve concurrent requests to the morechildren endpoint (see the note in the link), and returns a 429 when it does get concurrent requests. I was able to get a 429 error shortly after starting two instances of PRAW that simultaneously ran

for submission in reddit.subreddit(HIGH_TRAFFIC_SUB).top("all"):
    submission.comments.replace_more(limit= None)

I checked the ratelimit headers on the tape I made of this test and confirmed that the errors happened even with nearly all 600 requests remaining.

There are two kinds of 429 responses returned by Reddit: those that include retry-after headers, and those that don't. (Concurrent requests to the morechildren endpoint do not get retry-after headers). It wouldn't be hard to treat 429s as retry status if the response included a retry-after header, and as an exception status if the response didn't. This would be similar to how 5xx codes are treated. The rate limiter could be then adjusted to account for the presence of retry-after headers. However, since Reddit's rate limits are not always what they appear to be, this should be left for another day.

In the mean time, doing half of the above by just making 429s an exception status and adding a TooManyRequests class would not be a breaking change, since 429s already result in an assertion error.

The test test_request__too__many_requests__without_retry_headers was added in lieu of a test of concurrent requests to the morechildren endpoint, since the response bodies are the same.

References

Copy link
Copy Markdown
Member

@bboe bboe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the one suggestion I really like this PR. Thanks!

Comment thread prawcore/exceptions.py Outdated
Comment on lines +164 to +169
msg += (
f". Please wait at least {float(self.retry_after)} seconds "
"before re-trying this request."
if self.retry_after
else ""
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg += (
f". Please wait at least {float(self.retry_after)} seconds "
"before re-trying this request."
if self.retry_after
else ""
)
if self.retry_after:
msg += f". Please wait at least {float(self.retry_after)} seconds before re-trying this request."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that. I patterned it after the message in Redirect class, which also could be changed.

@bboe
Copy link
Copy Markdown
Member

bboe commented Jun 5, 2021

Looks like this needs a quick CHANGES.rst fix since the last merge.

@bboe
Copy link
Copy Markdown
Member

bboe commented Jun 5, 2021

Edit, since I'm here, I'll quickly fix this one so I can do a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants