mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-12-08 04:57:19 +00:00
Compare commits
3 commits
f09a05497b
...
93b983675c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93b983675c | ||
|
|
374d28cbf6 | ||
|
|
d515b9f16e |
|
|
@ -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
|
||||
|
|
@ -44,8 +48,12 @@ type Client struct {
|
|||
}
|
||||
|
||||
func NewClient() *Client {
|
||||
return NewClientWithDriver(httpdriver.NewClient())
|
||||
}
|
||||
|
||||
func NewClientWithDriver(driver httpdriver.Client) *Client {
|
||||
return &Client{
|
||||
Client: httpdriver.NewClient(),
|
||||
Client: driver,
|
||||
SchemaEncoder: &DefaultSchema{},
|
||||
Retries: Retries,
|
||||
context: context.Background(),
|
||||
|
|
@ -230,10 +238,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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue