Something + add comments to proto
This commit is contained in:
@@ -17,8 +17,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
baseURL = "https://musicbrainz.org/ws/2"
|
||||
userAgent = "MetadataAggregator/0.1.0 (https://github.com/metadata-agregator)"
|
||||
baseURL = "https://musicbrainz.org/ws/2"
|
||||
userAgent = "MetadataAggregator/0.1.0 (https://github.com/metadata-agregator)"
|
||||
maxRetries = 3
|
||||
baseBackoff = 2 * time.Second
|
||||
)
|
||||
|
||||
type client struct {
|
||||
@@ -36,16 +38,45 @@ func newClient() *client {
|
||||
}
|
||||
|
||||
func (c *client) get(ctx context.Context, endpoint string, params url.Values) ([]byte, error) {
|
||||
if err := c.waitForRateLimit(ctx); err != nil {
|
||||
return nil, err
|
||||
var lastErr error
|
||||
|
||||
for attempt := 0; attempt <= maxRetries; attempt++ {
|
||||
if err := c.waitForRateLimit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := c.buildRequest(ctx, endpoint, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := c.executeAndRead(ctx, req, endpoint)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
if err != ErrRateLimited {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lastErr = err
|
||||
if attempt < maxRetries {
|
||||
backoff := baseBackoff * time.Duration(1<<attempt)
|
||||
zerolog.Ctx(ctx).Info().
|
||||
Int("attempt", attempt+1).
|
||||
Int("max_retries", maxRetries).
|
||||
Dur("backoff", backoff).
|
||||
Msg("rate limited, waiting before retry")
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(backoff):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req, err := c.buildRequest(ctx, endpoint, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.executeAndRead(ctx, req, endpoint)
|
||||
return nil, lastErr
|
||||
}
|
||||
|
||||
func (c *client) waitForRateLimit(ctx context.Context) error {
|
||||
|
||||
Reference in New Issue
Block a user