fix(openai): guard 429-wrapped capacity shed against long Retry-After window

When a capacity-shed event (`slow_down` / `server_is_overloaded`) arrives with a
429 status and an explicit `Retry-After` exceeding the 8s same-account budget,
skip opening the 2-minute OAuth 429 retry deadline window as well, and emit
`upstream_retry_after` across WebSocket, Embeddings, and AlphaSearch logs.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
shawandDevin committed 2026-12-04 17:27:09 +08:00
1 parent d07f63ddf8
commit 332f45f858
5 files changed
+29 -8

No files matched your search

@@ -242,13 +242,14 @@ func (h *OpenAIGatewayHandler) AlphaSearch(c *gin.Context) {
if sameAccountRetryAllowed(failoverErr, sameAccountRetryCount[account.ID], retryLimit) {
sameAccountRetryCount[account.ID]++
retryDelay := sameAccountRetryDelayFor(failoverErr, sameAccountRetryCount[account.ID])
reqLog.Warn("openai_alpha_search.same_account_retry",
retryFields := appendUpstreamRetryAfterField([]zap.Field{
zap.Int64("account_id", account.ID),
zap.Int("upstream_status", failoverErr.StatusCode),
zap.Int("retry_limit", retryLimit),
zap.Int("retry_count", sameAccountRetryCount[account.ID]),
zap.Duration("retry_delay", retryDelay),
)
}, failoverErr)
reqLog.Warn("openai_alpha_search.same_account_retry", retryFields...)
select {
case <-c.Request.Context().Done():
return
@@ -269,11 +270,12 @@ func (h *OpenAIGatewayHandler) AlphaSearch(c *gin.Context) {
h.handleFailoverExhausted(c, failoverErr, false)
return
}
reqLog.Warn("openai_alpha_search.upstream_failover_switching",
switchFields := appendUpstreamRetryAfterField([]zap.Field{
zap.Int64("account_id", account.ID),
zap.Int("upstream_status", failoverErr.StatusCode),
zap.Int("switch_count", switchCount),
)
}, failoverErr)
reqLog.Warn("openai_alpha_search.upstream_failover_switching", switchFields...)
}
}
@@ -247,12 +247,13 @@ func (h *OpenAIGatewayHandler) Embeddings(c *gin.Context) {
return
}
switchCount++
reqLog.Warn("openai_embeddings.upstream_failover_switching",
switchFields := appendUpstreamRetryAfterField([]zap.Field{
zap.Int64("account_id", account.ID),
zap.Int("upstream_status", failoverErr.StatusCode),
zap.Int("switch_count", switchCount),
zap.Int("max_switches", maxAccountSwitches),
)
}, failoverErr)
reqLog.Warn("openai_embeddings.upstream_failover_switching", switchFields...)
continue
}
h.gatewayService.ReportOpenAIAccountScheduleResult(account, openAIAccountScheduleModel(c, account, reqModel, false, result), false, nil, err)
@@ -3081,12 +3081,13 @@ func (h *OpenAIGatewayHandler) ResponsesWebSocket(c *gin.Context) {
closeOpenAIWSFailoverExhausted(c, wsConn, failoverErr)
return false
}
reqLog.Warn("openai.websocket_upstream_failover_switching",
wsSwitchFields := appendUpstreamRetryAfterField([]zap.Field{
zap.Int64("account_id", account.ID),
zap.Int("upstream_status", failoverErr.StatusCode),
zap.Int("switch_count", switchCount),
zap.Int("max_switches", maxAccountSwitches),
)
}, failoverErr)
reqLog.Warn("openai.websocket_upstream_failover_switching", wsSwitchFields...)
if ctx.Err() != nil {
return false
}
@@ -254,6 +254,18 @@ func TestOpenAIStreamCapacityShedLongRetryAfterSkipsSameAccountReplay(t *testing
}
}
func TestOpenAIOAuth429WrappedCapacityShedLongRetryAfterSkipsDeadlineWindow(t *testing.T) {
svc := &OpenAIGatewayService{}
account := &Account{ID: 921, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
payload := []byte(`{"type":"error","error":{"type":"rate_limit_error","code":"slow_down","message":"Our servers are currently overloaded. Please try again later.","headers":{"retry-after":"315"}}}`)
err := svc.newOpenAIAccountFailoverError(account, http.StatusTooManyRequests, nil, payload, "Our servers are currently overloaded. Please try again later.", false, false)
require.True(t, err.RequestScopedTransient)
require.False(t, err.RetryableOnSameAccount)
require.True(t, err.SameAccountRetryDeadline.IsZero())
require.Equal(t, 315*time.Second, err.UpstreamRetryAfter)
}
// 上游降载的真实序列是「event: error → event: response.failed」。error 帧不算
// 客户端输出:若把它当首输出 flush,clientOutputStarted 被固化,随后的 failed
// 事件就进不了 pre-output failover 分支,只能把致命错误原样转发给客户端。
@@ -413,6 +413,11 @@ func (s *OpenAIGatewayService) newOpenAIAccountFailoverErrorWithClassificationHe
retryableOnSameAccount bool,
) *UpstreamFailoverError {
oauth429Retry := s.shouldRetryOpenAIOAuth429OnSameAccountWithResponse(account, statusCode, shouldDisable, classificationHeaders, responseBody)
if oauth429Retry && isOpenAIRequestScopedCapacityShed(upstreamMsg, responseBody) {
if upstreamRetryAfter, ok := parseUpstreamRetryAfterDelay(responseHeaders, responseBody); ok && !openAICapacityShedAllowsSameAccountRetry(upstreamRetryAfter) {
oauth429Retry = false
}
}
failoverErr := newOpenAIUpstreamFailoverError(
statusCode,
responseHeaders,