Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions internal/jev/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error
// without ever contacting the server.
attempts := max(c.MaxRetries, 0) + 1
var last error
forbiddenRetried := false
for attempt := range attempts {
if err := c.pace(ctx); err != nil {
return exchange, &APIError{Message: "cancelled: " + err.Error()}
Expand All @@ -87,9 +88,20 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error
last = err

var apiErr *APIError
if !errors.As(err, &apiErr) || !apiErr.Retryable || attempt == attempts-1 {
if !errors.As(err, &apiErr) || attempt == attempts-1 {
break
}
// A 403 is usually a real refusal, but the provider behind OpenRouter
// has also returned a transient "RBAC: access denied" for a valid key.
// Retry it exactly once, after a short pause; a second 403 is final.
retryForbidden := apiErr.Status == http.StatusForbidden && !forbiddenRetried
if !apiErr.Retryable && !retryForbidden {
break
}
if retryForbidden {
forbiddenRetried = true
wait = forbiddenRetryWait
}
if wait <= 0 {
wait = c.backoff(attempt)
}
Expand All @@ -103,6 +115,9 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error
return exchange, last
}

// forbiddenRetryWait is the pause before the single retry of a 403.
const forbiddenRetryWait = 2 * time.Second

// once performs a single attempt. The returned duration is a server-requested
// wait, when one was given.
func (c *Client) once(ctx context.Context, body []byte) (Response, time.Duration, error) {
Expand Down Expand Up @@ -156,7 +171,7 @@ func (c *Client) once(ctx context.Context, body []byte) (Response, time.Duration

// retryableStatus: 429 and 529 ask the client to back off and retry, and a 5xx
// is a server fault worth another try. A 401 or 422 fails the same way every
// time.
// time. A 403 is not retryable here; Evaluate gives it one extra attempt.
func retryableStatus(status int) bool {
switch {
case status == http.StatusTooManyRequests, status == 529:
Expand Down
33 changes: 33 additions & 0 deletions internal/jev/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,39 @@ func TestClientDoesNotRetryAuthFailureAndRedactsKey(t *testing.T) {
}
}

// A transient provider 403 ("RBAC: access denied" for a valid key) gets one
// retry; a lasting 403 fails after exactly two attempts, never more.
func TestClientRetriesOneForbiddenOnly(t *testing.T) {
transient := newFakeServer(t, func(n int) (int, http.Header, string) {
if n == 1 {
return http.StatusForbidden, nil, `{"error":{"message":"HTTP 403: RBAC: access denied","code":403}}`
}
return http.StatusOK, nil, okAnswer
})
var waits []time.Duration
client := testClient(transient.server.URL)
client.Sleep = func(_ context.Context, d time.Duration) error { waits = append(waits, d); return nil }
exchange, err := client.Evaluate(context.Background(), Request{Model: DefaultModel})
if err != nil || exchange.Attempts != 2 {
t.Fatalf("transient 403: attempts = %d, err = %v; want success on attempt 2", exchange.Attempts, err)
}
if len(waits) != 1 || waits[0] != forbiddenRetryWait {
t.Fatalf("waits = %v, want [%v]", waits, forbiddenRetryWait)
}

lasting := newFakeServer(t, func(int) (int, http.Header, string) {
return http.StatusForbidden, nil, "denied"
})
exchange, err = testClient(lasting.server.URL).Evaluate(context.Background(), Request{Model: DefaultModel})
var apiErr *APIError
if !errors.As(err, &apiErr) || apiErr.Status != http.StatusForbidden {
t.Fatalf("lasting 403: err = %v", err)
}
if exchange.Attempts != 2 {
t.Fatalf("lasting 403: attempts = %d, want 2 (MaxRetries is %d)", exchange.Attempts, testClient("").MaxRetries)
}
}

func TestClientCapsRetryAfterAtMaxBackoff(t *testing.T) {
server := newFakeServer(t, func(n int) (int, http.Header, string) {
if n == 1 {
Expand Down
Loading