Skip to content

Commit

Permalink
Allow retry period to be cancelled by context
Browse files Browse the repository at this point in the history
Signed-off-by: Russell Troxel <russelltroxel@gmail.com>
  • Loading branch information
rtrox committed Dec 28, 2022
1 parent caeae58 commit a299ace
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,12 @@ func (c *Client) execute(req *http.Request, result interface{}, needsStatus ...i
defer resp.Body.Close()

if c.autoRetry && shouldRetry(resp.StatusCode) {
time.Sleep(retryDuration(resp))
continue
select {
case <-req.Context().Done():
// If the context is cancelled, return the original error
case <-time.After(retryDuration(resp)):
continue
}
}
if resp.StatusCode == http.StatusNoContent {
return nil
Expand Down Expand Up @@ -266,8 +270,12 @@ func (c *Client) get(ctx context.Context, url string, result interface{}) error
defer resp.Body.Close()

if resp.StatusCode == rateLimitExceededStatusCode && c.autoRetry {
time.Sleep(retryDuration(resp))
continue
select {
case <-ctx.Done():
// If the context is cancelled, return the original error
case <-time.After(retryDuration(resp)):
continue
}
}
if resp.StatusCode == http.StatusNoContent {
return nil
Expand Down

0 comments on commit a299ace

Please sign in to comment.