mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(retry): reject non-positive backoff
A zero or negative backoff passed validation and made retries fire with no pause. The config loader now rejects it. The wait ladder compares the backoff to the cap before it multiplies, so a very large backoff cannot overflow.
This commit is contained in:
@@ -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)<<uint(attempt-1))
|
||||
if wait > maxRetryWait {
|
||||
wait = maxRetryWait
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user