mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
More resilient HTTP requests
Fixed rate limit check for ListenBrainz and Funkwhale. Also retry always on server side errors.
This commit is contained in:
parent
240351dc3e
commit
4e9f50b6b6
3 changed files with 28 additions and 12 deletions
|
@ -31,6 +31,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxItemsPerGet = 50
|
const MaxItemsPerGet = 50
|
||||||
|
const DefaultRateLimitWaitSeconds = 5
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
HttpClient *resty.Client
|
HttpClient *resty.Client
|
||||||
|
@ -48,12 +49,20 @@ func NewClient(serverUrl string, token string) Client {
|
||||||
client.SetRetryCount(5)
|
client.SetRetryCount(5)
|
||||||
client.AddRetryCondition(
|
client.AddRetryCondition(
|
||||||
func(r *resty.Response, err error) bool {
|
func(r *resty.Response, err error) bool {
|
||||||
return r.StatusCode() == http.StatusTooManyRequests
|
code := r.StatusCode()
|
||||||
|
return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
||||||
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
||||||
retryAfter, err := strconv.Atoi(resp.Header().Get("X-RateLimit-Reset-In"))
|
var err error
|
||||||
|
var retryAfter int = DefaultRateLimitWaitSeconds
|
||||||
|
if resp.StatusCode() == http.StatusTooManyRequests {
|
||||||
|
retryAfter, err = strconv.Atoi(resp.Header().Get("Retry-After"))
|
||||||
|
if err != nil {
|
||||||
|
retryAfter = DefaultRateLimitWaitSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
return time.Duration(retryAfter * int(time.Second)), err
|
return time.Duration(retryAfter * int(time.Second)), err
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ const listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
|
||||||
|
|
||||||
const DefaultItemsPerGet = 25
|
const DefaultItemsPerGet = 25
|
||||||
const MaxItemsPerGet = 1000
|
const MaxItemsPerGet = 1000
|
||||||
|
const DefaultRateLimitWaitSeconds = 5
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
HttpClient *resty.Client
|
HttpClient *resty.Client
|
||||||
|
@ -46,18 +47,25 @@ func NewClient(token string) Client {
|
||||||
client.SetAuthScheme("Token")
|
client.SetAuthScheme("Token")
|
||||||
client.SetAuthToken(token)
|
client.SetAuthToken(token)
|
||||||
client.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "application/json")
|
||||||
client.SetHeader("Content-Type", "application/json")
|
|
||||||
|
|
||||||
// Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting)
|
// Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting)
|
||||||
client.SetRetryCount(5)
|
client.SetRetryCount(5)
|
||||||
client.AddRetryCondition(
|
client.AddRetryCondition(
|
||||||
func(r *resty.Response, err error) bool {
|
func(r *resty.Response, err error) bool {
|
||||||
return r.StatusCode() == http.StatusTooManyRequests
|
code := r.StatusCode()
|
||||||
|
return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
||||||
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
||||||
retryAfter, err := strconv.Atoi(resp.Header().Get("Retry-After"))
|
var err error
|
||||||
|
var retryAfter int = DefaultRateLimitWaitSeconds
|
||||||
|
if resp.StatusCode() == http.StatusTooManyRequests {
|
||||||
|
retryAfter, err = strconv.Atoi(resp.Header().Get("X-RateLimit-Reset-In"))
|
||||||
|
if err != nil {
|
||||||
|
retryAfter = DefaultRateLimitWaitSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
return time.Duration(retryAfter * int(time.Second)), err
|
return time.Duration(retryAfter * int(time.Second)), err
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -34,15 +34,14 @@ type Client struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(serverUrl string, token string) Client {
|
func NewClient(serverUrl string, token string) Client {
|
||||||
resty := resty.New()
|
client := resty.New()
|
||||||
resty.SetBaseURL(serverUrl)
|
client.SetBaseURL(serverUrl)
|
||||||
resty.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "application/json")
|
||||||
client := Client{
|
client.SetRetryCount(5)
|
||||||
HttpClient: resty,
|
return Client{
|
||||||
|
HttpClient: client,
|
||||||
token: token,
|
token: token,
|
||||||
}
|
}
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult, err error) {
|
func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult, err error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue