diff --git a/pkg/ratelimit/httpheader.go b/pkg/ratelimit/httpheader.go index dba5e30..617c3b8 100644 --- a/pkg/ratelimit/httpheader.go +++ b/pkg/ratelimit/httpheader.go @@ -1,5 +1,5 @@ /* -Copyright © 2023 Philipp Wolfer +Copyright © 2023-2025 Philipp Wolfer 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 @@ -25,9 +25,9 @@ import ( ) const ( - RetryCount = 5 - DefaultRateLimitWaitSeconds = 5 - MaxWaitTimeSeconds = 60 + RetryCount = 5 + DefaultRateLimitWait = 5 * time.Second + MaxWaitTime = 60 * time.Second ) // Implements rate HTTP header based limiting for resty. @@ -47,16 +47,15 @@ func EnableHTTPHeaderRateLimit(client *resty.Client, resetInHeader string) { return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError }, ) - client.SetRetryMaxWaitTime(time.Duration(MaxWaitTimeSeconds * time.Second)) + client.SetRetryMaxWaitTime(MaxWaitTime) client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) { - var err error - var retryAfter int = DefaultRateLimitWaitSeconds + retryAfter := DefaultRateLimitWait if resp.StatusCode() == http.StatusTooManyRequests { - retryAfter, err = strconv.Atoi(resp.Header().Get(resetInHeader)) - if err != nil { - retryAfter = DefaultRateLimitWaitSeconds + retryAfterHeader, err := strconv.Atoi(resp.Header().Get(resetInHeader)) + if err == nil { + retryAfter = time.Duration(retryAfterHeader) * time.Second } } - return time.Duration(retryAfter * int(time.Second)), err + return retryAfter, nil }) }