1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-12-08 04:57:19 +00:00

sleep on rate limit

This commit is contained in:
Merlin 2025-03-21 19:53:25 +01:00
parent e47e27bd82
commit d515b9f16e
2 changed files with 30 additions and 1 deletions

View file

@ -2,3 +2,4 @@ golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

View file

@ -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 {
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)
if err == nil {
time.Sleep(time.Duration(errBody.RetryAfter) * time.Second)
continue
} else {
doErr = fmt.Errorf("failed to parse rate limit body: %w", err)
continue
}
}
break
}