diff --git a/pkg/utils/http_retry.go b/pkg/utils/http_retry.go index 1ad904064..e90fa2129 100644 --- a/pkg/utils/http_retry.go +++ b/pkg/utils/http_retry.go @@ -9,6 +9,8 @@ import ( const maxRetries = 3 +var retryDelayUnit = time.Second + func shouldRetry(statusCode int) bool { return statusCode == http.StatusTooManyRequests || statusCode >= 500 @@ -34,7 +36,7 @@ func DoRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, } if i < maxRetries-1 { - if err = sleepWithCtx(req.Context(), time.Second*time.Duration(i+1)); err != nil { + if err = sleepWithCtx(req.Context(), retryDelayUnit*time.Duration(i+1)); err != nil { return nil, fmt.Errorf("failed to sleep: %w", err) } } diff --git a/pkg/utils/http_retry_test.go b/pkg/utils/http_retry_test.go index 51af33038..b850d263b 100644 --- a/pkg/utils/http_retry_test.go +++ b/pkg/utils/http_retry_test.go @@ -11,6 +11,9 @@ import ( ) func TestDoRequestWithRetry(t *testing.T) { + retryDelayUnit = time.Millisecond + t.Cleanup(func() { retryDelayUnit = time.Second }) + testcases := []struct { name string serverBehavior func(*httptest.Server) int @@ -53,6 +56,7 @@ func TestDoRequestWithRetry(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { + t.Parallel() attempts := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { attempts++ @@ -91,6 +95,9 @@ func TestDoRequestWithRetry(t *testing.T) { } func TestDoRequestWithRetry_Delay(t *testing.T) { + retryDelayUnit = time.Millisecond + t.Cleanup(func() { retryDelayUnit = time.Second }) + var start time.Time delays := []time.Duration{} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -124,5 +131,5 @@ func TestDoRequestWithRetry_Delay(t *testing.T) { assert.Equal(t, http.StatusOK, resp.StatusCode) resp.Body.Close() - assert.GreaterOrEqual(t, delays[2], time.Second) + assert.GreaterOrEqual(t, delays[2], time.Millisecond) }