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
Discourse API throttling. #11
Comments
Reference to the relevant announcement. https://meta.discourse.org/t/global-rate-limits-in-discourse/78612
|
If nothing else, it would be good to add a specific Exception class to represent the 429 status. I'd call the the 90% solution. I'm not strictly opposed to adding throttling, but that sounds like a second step, and a potentially more complicated step with a lot smaller payoff. It'd have to be configurable and/or opt-in, since the limits can be reconfigured per-Discourse deployment. Are you thinking of adding a rate limiter or an explicit request delay? |
(I replied with this on the 30th, apparently it didn't make it to GitHub. I haven't had time to follow this up any further but may do in the next few weeks). Good question. Thinking about it, the latter: I was hoping they sent the number of seconds to delay in the errors {"errors":["We have a daily limit on how many times that action can be thanks, |
Following my request, Discourse have added a separate field to the response which includes the back off period. I can't test this until they've upgraded our discourse instance, but I'm guessing something like this with a sleep() will be needed.
|
It'd probably be a good idea to extract - given that it's the format "X seconds" a regex match should suffice.
In general I'd use a loop (usually a Consider this a napkin sketch: while retry_count > 1:
response = requests.request(...)
if 400 <= response.status_code < 500:
if response.status_code == 429:
logger.warning("429 response message")
wait_seconds = parse_wait_seconds(response)
if retry_count > 1:
time.sleep(wait_seconds)
retry_count -= 1
continue #
else:
# All other 4xx options return or raise
elif:
# All other code options return or raise
raise DiscourseRateLimitedError("429!", response=response) This has a value premise: Every response other than a 429 must either result in a return value or raising an exception from within the 429 responses should be logged with a warning, before final success or exception, as this is important information to know for an API user. |
Per the announcement on Discourse meta, global API rate limits have been introduced to the Discourse API. This change adds a new DiscourseRateLimitedError class and a retry mechanism on receipt of a 429. https://meta.discourse.org/t/global-rate-limits-in-discourse/78612 Closes: pydiscourse#11
In the last month or so Discourse implemented API throttling.
I don't have a lot of detail yet but I was told
Are you open to building the throttling directly in to _request or would you prefer library users handled it individually?
https://github.com/bennylope/pydiscourse/blob/master/pydiscourse/client.py#L1186
At the moment an error along these lines is raised (this is what caused me to ask Discourse).
thanks,
kk
The text was updated successfully, but these errors were encountered: