mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +02:00
Refactored common rate limit code into separate module
This commit is contained in:
parent
857661ebf9
commit
0f5cb49b4c
4 changed files with 69 additions and 71 deletions
|
@ -23,15 +23,13 @@ package funkwhale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/ratelimit"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxItemsPerGet = 50
|
const MaxItemsPerGet = 50
|
||||||
const DefaultRateLimitWaitSeconds = 5
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
HttpClient *resty.Client
|
HttpClient *resty.Client
|
||||||
|
@ -46,25 +44,7 @@ func NewClient(serverUrl string, token string) Client {
|
||||||
client.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "application/json")
|
||||||
|
|
||||||
// Handle rate limiting (see https://docs.funkwhale.audio/developer/api/rate-limit.html)
|
// Handle rate limiting (see https://docs.funkwhale.audio/developer/api/rate-limit.html)
|
||||||
client.SetRetryCount(5)
|
ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After")
|
||||||
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("Retry-After"))
|
|
||||||
if err != nil {
|
|
||||||
retryAfter = DefaultRateLimitWaitSeconds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return time.Duration(retryAfter * int(time.Second)), err
|
|
||||||
})
|
|
||||||
|
|
||||||
return Client{
|
return Client{
|
||||||
HttpClient: client,
|
HttpClient: client,
|
||||||
|
|
|
@ -23,20 +23,18 @@ package listenbrainz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/ratelimit"
|
||||||
)
|
)
|
||||||
|
|
||||||
const listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultItemsPerGet = 25
|
listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
|
||||||
MaxItemsPerGet = 1000
|
DefaultItemsPerGet = 25
|
||||||
MaxListensPerRequest = 1000
|
MaxItemsPerGet = 1000
|
||||||
DefaultRateLimitWaitSeconds = 5
|
MaxListensPerRequest = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -52,25 +50,7 @@ func NewClient(token string) Client {
|
||||||
client.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "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)
|
ratelimit.EnableHTTPHeaderRateLimit(client, "X-RateLimit-Reset-In")
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
return Client{
|
return Client{
|
||||||
HttpClient: client,
|
HttpClient: client,
|
||||||
|
|
|
@ -25,17 +25,18 @@ package spotify
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/ratelimit"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const baseURL = "https://api.spotify.com/v1/"
|
const (
|
||||||
const MaxItemsPerGet = 50
|
baseURL = "https://api.spotify.com/v1/"
|
||||||
const DefaultRateLimitWaitSeconds = 5
|
MaxItemsPerGet = 50
|
||||||
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
HttpClient *resty.Client
|
HttpClient *resty.Client
|
||||||
|
@ -47,25 +48,10 @@ func NewClient(token oauth2.TokenSource) Client {
|
||||||
client := resty.NewWithClient(httpClient)
|
client := resty.NewWithClient(httpClient)
|
||||||
client.SetBaseURL(baseURL)
|
client.SetBaseURL(baseURL)
|
||||||
client.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "application/json")
|
||||||
client.SetRetryCount(5)
|
|
||||||
client.AddRetryCondition(
|
// Handle rate limiting (see https://developer.spotify.com/documentation/web-api/concepts/rate-limits)
|
||||||
func(r *resty.Response, err error) bool {
|
ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After")
|
||||||
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("Retry-After"))
|
|
||||||
if err != nil {
|
|
||||||
retryAfter = DefaultRateLimitWaitSeconds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return time.Duration(retryAfter * int(time.Second)), err
|
|
||||||
})
|
|
||||||
return Client{
|
return Client{
|
||||||
HttpClient: client,
|
HttpClient: client,
|
||||||
}
|
}
|
||||||
|
|
52
internal/ratelimit/httpheader.go
Normal file
52
internal/ratelimit/httpheader.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||||
|
|
||||||
|
Scotty is free software: you can redistribute it and/or modify it under the
|
||||||
|
terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package ratelimit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
RetryCount = 5
|
||||||
|
DefaultRateLimitWaitSeconds = 5
|
||||||
|
MaxWaitTimeSeconds = 60
|
||||||
|
)
|
||||||
|
|
||||||
|
func EnableHTTPHeaderRateLimit(client *resty.Client, resetInHeader string) {
|
||||||
|
client.SetRetryCount(RetryCount)
|
||||||
|
client.AddRetryCondition(
|
||||||
|
func(r *resty.Response, err error) bool {
|
||||||
|
code := r.StatusCode()
|
||||||
|
return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError
|
||||||
|
},
|
||||||
|
)
|
||||||
|
client.SetRetryMaxWaitTime(time.Duration(MaxWaitTimeSeconds * time.Second))
|
||||||
|
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(resetInHeader))
|
||||||
|
if err != nil {
|
||||||
|
retryAfter = DefaultRateLimitWaitSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return time.Duration(retryAfter * int(time.Second)), err
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue