diff --git a/retry.go b/retry.go index 545b7a1..fa7ee74 100644 --- a/retry.go +++ b/retry.go @@ -171,7 +171,7 @@ func defaultWaitFn(ctx context.Context, d time.Duration) bool { // tries total); the maximum, maxRetryAttempts, is 3 retries (four tries // total). A value outside 1..maxRetryAttempts is a config error. // rc.Backoff defaults to defaultRetryBackoff when left empty, and must -// parse via time.ParseDuration either way. +// parse via time.ParseDuration to a positive duration either way. func newRetryPolicy(rc RetryConfig) (*retryPolicy, error) { if !rc.Enabled { return &retryPolicy{enabled: false, waitFn: defaultWaitFn}, nil @@ -193,6 +193,9 @@ func newRetryPolicy(rc RetryConfig) (*retryPolicy, error) { if err != nil { return nil, fmt.Errorf("llmgateway: retry.backoff: %w", err) } + if backoff <= 0 { + return nil, fmt.Errorf("llmgateway: retry.backoff must be positive, got %q", backoffStr) + } return &retryPolicy{enabled: true, attempts: attempts, backoff: backoff, waitFn: defaultWaitFn}, nil } @@ -263,6 +266,10 @@ func (p *retryPolicy) waitBefore(attempt int, resp *http.Response) (wait time.Du } } + // Compare before multiplying: a backoff at or above the cap must not overflow the shift. + if p.backoff >= maxRetryWait { + return maxRetryWait, false + } wait = p.backoff * time.Duration(int64(1)< maxRetryWait { wait = maxRetryWait diff --git a/retry_test.go b/retry_test.go index ca7ec75..165c999 100644 --- a/retry_test.go +++ b/retry_test.go @@ -1087,3 +1087,52 @@ func TestRetryPolicy_Do_NoAttemptRecorderInContext_NoPanic(t *testing.T) { t.Errorf("resp.StatusCode = %d, want 200", resp.StatusCode) } } + +// TestNewRetryPolicy_RejectsNonPositiveBackoff pins that a zero or +// negative backoff is a config error, not a silent no-wait retry. +func TestNewRetryPolicy_RejectsNonPositiveBackoff(t *testing.T) { + t.Parallel() + tests := []struct { + name string + backoff string + wantErr bool + }{ + {name: "zero", backoff: "0s", wantErr: true}, + {name: "negative", backoff: "-1s", wantErr: true}, + {name: "positive", backoff: "1ms", wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + _, err := newRetryPolicy(RetryConfig{Enabled: true, Backoff: tt.backoff}) + if (err != nil) != tt.wantErr { + t.Errorf("newRetryPolicy(Backoff:%q) err = %v, wantErr %v", tt.backoff, err, tt.wantErr) + } + }) + } +} + +// TestRetryPolicy_WaitBeforeLargeBackoffCapped pins that a backoff at or +// above the cap returns the cap for every attempt, without overflowing. +func TestRetryPolicy_WaitBeforeLargeBackoffCapped(t *testing.T) { + t.Parallel() + tests := []struct { + name string + backoff time.Duration + }{ + {name: "equal to cap", backoff: maxRetryWait}, + {name: "int64 overflow range", backoff: time.Duration(1) << 62}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + p := &retryPolicy{enabled: true, attempts: maxRetryAttempts, backoff: tt.backoff, waitFn: defaultWaitFn} + for attempt := 1; attempt <= maxRetryAttempts; attempt++ { + got, fromRetryAfter := p.waitBefore(attempt, nil) + if got != maxRetryWait || fromRetryAfter { + t.Errorf("attempt %d: waitBefore = (%v, %v), want (%v, false)", attempt, got, fromRetryAfter, maxRetryWait) + } + } + }) + } +}