Catch new errors and fix back off function

This commit is contained in:
Ben Adrian Sarmiento
2024-07-12 17:23:59 +02:00
parent 0f0bdfcb5a
commit 7d6deba5bd

View File

@@ -67,7 +67,7 @@ func NewHTTPClient(
client: &http.Client{}, client: &http.Client{},
maxRetries: maxRetries, maxRetries: maxRetries,
timeoutSecs: timeoutSecs, timeoutSecs: timeoutSecs,
rateLimitSleepSecs: 4, rateLimitSleepSecs: 1,
backoff: backoffFunc, backoff: backoffFunc,
dnsCache: cmap.New[string](), dnsCache: cmap.New[string](),
hosts: hosts, hosts: hosts,
@@ -269,14 +269,20 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
time.Sleep(secs) time.Sleep(secs)
return true return true
case -1: // Internal error case -1: // Internal error
if attempts >= r.maxRetries {
r.log.Debugf("RD Internal error, attempt #%d", attempts+1)
return false
}
secs := r.backoff(attempts, 1) secs := r.backoff(attempts, 1)
r.log.Debugf("RD Internal error, attempt #%d, retrying in %d seconds", attempts+1, secs/time.Second) r.log.Debugf("RD Internal error, attempt #%d, retrying in %d seconds", attempts+1, secs/time.Second)
time.Sleep(secs) time.Sleep(secs)
return true return true
case 429:
secs := r.backoff(attempts, rateLimitSleep)
r.log.Debugf("API rate limit reached, attempt #%d, retrying in %d seconds", attempts+1, secs/time.Second)
time.Sleep(secs)
return true
case 503:
secs := r.backoff(attempts, rateLimitSleep)
r.log.Debugf("RD Service Unavailable, attempt #%d, retrying in %d seconds", attempts+1, secs/time.Second)
time.Sleep(secs)
return true
default: default:
return false return false
} }
@@ -319,7 +325,7 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
func backoffFunc(attempt, base int) time.Duration { func backoffFunc(attempt, base int) time.Duration {
maxDuration := 60 maxDuration := 60
backoff := int(math.Pow(float64(base), float64(attempt+1))) backoff := base * int(math.Pow(2, float64(attempt)))
if backoff > maxDuration { if backoff > maxDuration {
backoff = maxDuration backoff = maxDuration
} }