openai-go
openai-go copied to clipboard
fix: prevent panic in retryDelay when delay overflows or is negative
Description
This PR fixes a panic in retryDelay that occurs when the computed delay becomes negative due to integer overflow in time.Duration for large retryCount values
Problem
- The existing exponential backoff calculation (
0.5 * 2^retryCount * time.Second) can exceed the maximumint64fortime.Duration, wrapping around to negative values. - While I was using the API, the panic was observed at
retryCount = 35, where the computeddelaywrapped to-9223372036854775808. - When
delayis negative, the subsequentrand.Int63n(delay/4)call panics.
Fix
- Clamp
delayto a safe range[1s, maxDelay]before applying jitter. - Ensure the jitter argument to
rand.Int63nis always >= 1 second, preventing it from receiving invalid (<=0) arguments. - Subtract jitter safely from the clamped delay.
Impact
- Prevents panics on high retry counts.
- Maintains existing exponential backoff behavior within reasonable limits.