-
-
Notifications
You must be signed in to change notification settings - Fork 64
Respect retry_after in HTTP 429 response
#470
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
base: v3
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,10 @@ import ( | |||||
| // StatusTooManyRequests is the HTTP status code discord sends on rate-limiting. | ||||||
| const StatusTooManyRequests = 429 | ||||||
|
|
||||||
| // StatusNirnTimeout is the HTTP status code Nirn Proxy sends when the request | ||||||
| // times out. | ||||||
| const StatusNirnTimeout = 408 | ||||||
|
|
||||||
| // Retries is the default attempts to retry if the API returns an error before | ||||||
| // giving up. If the value is smaller than 1, then requests will retry forever. | ||||||
| var Retries uint = 5 | ||||||
|
|
@@ -230,10 +234,34 @@ func (c *Client) request( | |||||
| continue | ||||||
| } | ||||||
|
|
||||||
| if status = r.GetStatus(); status == StatusTooManyRequests || status >= 500 { | ||||||
| status = r.GetStatus() | ||||||
|
|
||||||
| if status == StatusNirnTimeout || status >= 500 { | ||||||
|
Owner
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. Rather than adding a |
||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| if status == StatusTooManyRequests { | ||||||
| var body = r.GetBody() | ||||||
| defer body.Close() | ||||||
|
|
||||||
| buf := bytes.Buffer{} | ||||||
| buf.ReadFrom(body) | ||||||
|
|
||||||
| var errBody struct { | ||||||
| RetryAfter float32 `json:"retry_after"` | ||||||
| } | ||||||
|
|
||||||
| err := json.Unmarshal(buf.Bytes(), &errBody) | ||||||
|
Owner
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. We can shorten this to:
Suggested change
|
||||||
|
|
||||||
| if err == nil { | ||||||
|
Owner
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. Happy path: put the |
||||||
| time.Sleep(time.Duration(errBody.RetryAfter) * time.Second) | ||||||
|
Owner
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.
select {
case <-ctx.Done():
doErr = ctx.Err()
continue // or break out of the loop, but this will work too
case <-time.After(...):
continue
} |
||||||
| continue | ||||||
| } else { | ||||||
| doErr = fmt.Errorf("failed to parse rate limit body: %w", err) | ||||||
| continue | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| break | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
In the case of a 5xx response, I think we should do our own exponential backoff to prevent spamming Discord.