Skip to content

Conversation

@merlinfuchs
Copy link

Arikawa does prevent most 429's by respecting the headers returned by Discord but some rate limits aren't indicated by the headers.

if status = r.GetStatus(); status == StatusTooManyRequests || status >= 500 {
status = r.GetStatus()

if status == StatusNirnTimeout || status >= 500 {
Copy link
Owner

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.

if status = r.GetStatus(); status == StatusTooManyRequests || status >= 500 {
status = r.GetStatus()

if status == StatusNirnTimeout || status >= 500 {
Copy link
Owner

Choose a reason for hiding this comment

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

Rather than adding a StatusNirnTimeout, I think we should just write status == http.StatusRequestTimeout.

err := json.Unmarshal(buf.Bytes(), &errBody)

if err == nil {
time.Sleep(time.Duration(errBody.RetryAfter) * time.Second)
Copy link
Owner

Choose a reason for hiding this comment

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

time.Sleep is not context-aware. Prefer writing:

select {
case <-ctx.Done():
    doErr = ctx.Err()
    continue // or break out of the loop, but this will work too
case <-time.After(...):
    continue
}

RetryAfter float32 `json:"retry_after"`
}

err := json.Unmarshal(buf.Bytes(), &errBody)
Copy link
Owner

Choose a reason for hiding this comment

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

We can shorten this to:

Suggested change
err := json.Unmarshal(buf.Bytes(), &errBody)
err := json.NewDecoder(body).Decode(&errBody)


err := json.Unmarshal(buf.Bytes(), &errBody)

if err == nil {
Copy link
Owner

Choose a reason for hiding this comment

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

Happy path: put the if err != nil check first, then put the other case after the check.

@merlinfuchs
Copy link
Author

merlinfuchs commented Mar 21, 2025

I mainly made these changes so I can it in a project, I agree with your comments and can make the changes when I get around to it! Happy for you to also close this PR and make the changes :)

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