Skip to content

Fix Retry-After handling #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions pkg/rest/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rest

import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -50,21 +51,18 @@ func Head(ctx context.Context, url, token string) error {

case http.StatusTooManyRequests:
// Get the retry after header, parse it to int64 and set a timer until retry
retryAfterMillis := response.Header.Get("Retry-After")
retryAfterMillisInt, _ := strconv.ParseInt(retryAfterMillis, 10, 64)
retryAfter, _ := strconv.ParseInt(response.Header.Get("Retry-After"), 10, 64)

// Convert the retry after millis to a time.Time object and calculate the time until retry
retryAfter := time.Unix(retryAfterMillisInt, 0)
timeUntilRetry := time.Until(retryAfter)

logger.Log(ctx, slog.LevelWarn, "request was rate limited", slog.String("timeUntilRetry", timeUntilRetry.String()))
time.Sleep(timeUntilRetry)

continue
if retryAfter > 0 {
logger.Log(ctx, slog.LevelWarn, "request was rate limited", slog.Int64("retryAfter", retryAfter))
time.Sleep(time.Duration(retryAfter) * time.Second)
continue
}
return errors.New("request was rate-limited (429 Too Many Requests) without Retry-After")

case http.StatusNotFound:
logger.Log(ctx, slog.LevelDebug, "resource not found", slog.String("url", url))
return fmt.Errorf("not found")
return errors.New("not found")

default:
if response.StatusCode >= 500 && response.StatusCode < 600 {
Expand Down
Loading