Refactored common rate limit code into separate module

This commit is contained in:
Philipp Wolfer 2023-11-24 00:22:36 +01:00
parent 857661ebf9
commit 0f5cb49b4c
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
4 changed files with 69 additions and 71 deletions

View file

@ -23,20 +23,18 @@ package listenbrainz
import (
"errors"
"net/http"
"strconv"
"time"
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/ratelimit"
)
const listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
const (
DefaultItemsPerGet = 25
MaxItemsPerGet = 1000
MaxListensPerRequest = 1000
DefaultRateLimitWaitSeconds = 5
listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
DefaultItemsPerGet = 25
MaxItemsPerGet = 1000
MaxListensPerRequest = 1000
)
type Client struct {
@ -52,25 +50,7 @@ func NewClient(token string) Client {
client.SetHeader("Accept", "application/json")
// Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting)
client.SetRetryCount(5)
client.AddRetryCondition(
func(r *resty.Response, err error) bool {
code := r.StatusCode()
return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError
},
)
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
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
})
ratelimit.EnableHTTPHeaderRateLimit(client, "X-RateLimit-Reset-In")
return Client{
HttpClient: client,