utils: make retry-after numeric clamp overflow-safe

This commit is contained in:
Alix-007
2026-03-30 22:02:16 +08:00
parent 9440bebca6
commit 345d4fddc9
2 changed files with 60 additions and 10 deletions
+14 -2
View File
@@ -60,8 +60,8 @@ func retryDelayForAttempt(resp *http.Response, attempt int) time.Duration {
return clampRetryDelay(fallback)
}
if seconds, err := strconv.Atoi(retryAfter); err == nil && seconds >= 0 {
return clampRetryDelay(time.Duration(seconds) * time.Second)
if delay, ok := numericRetryAfterDelay(retryAfter); ok {
return delay
}
if when, err := http.ParseTime(retryAfter); err == nil {
@@ -78,6 +78,18 @@ func retryDelayForAttempt(resp *http.Response, attempt int) time.Duration {
return clampRetryDelay(fallback)
}
func numericRetryAfterDelay(retryAfter string) (time.Duration, bool) {
seconds, err := strconv.ParseInt(retryAfter, 10, 64)
if err != nil || seconds < 0 {
return 0, false
}
maxSeconds := int64(maxRetrySleepDuration / time.Second)
if seconds > maxSeconds {
return maxRetrySleepDuration, true
}
return clampRetryDelay(time.Duration(seconds) * time.Second), true
}
func clampRetryDelay(delay time.Duration) time.Duration {
if delay <= 0 {
return 0