Skip to content
Closed
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
1 change: 1 addition & 0 deletions constant/context_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
ContextKeyAutoGroup ContextKey = "auto_group"
ContextKeyAutoGroupIndex ContextKey = "auto_group_index"
ContextKeyAutoGroupRetryIndex ContextKey = "auto_group_retry_index"
ContextKeyRouteAutoGroups ContextKey = "route_auto_groups"

/* user related keys */
ContextKeyUserId ContextKey = "id"
Expand Down
7 changes: 7 additions & 0 deletions controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {

newAPIError = service.NormalizeViolationFeeError(newAPIError)
relayInfo.LastError = newAPIError
if types.IsClientCanceledError(newAPIError) {
logger.LogInfo(c, fmt.Sprintf("client canceled request, skip channel retry: %s", common.LocalLogPreview(newAPIError.Error())))
break
}

processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError)

Expand Down Expand Up @@ -326,6 +330,9 @@ func shouldRetry(c *gin.Context, openaiErr *types.NewAPIError, retryTimes int) b
if openaiErr == nil {
return false
}
if types.IsClientCanceledError(openaiErr) {
return false
}
if service.ShouldSkipRetryAfterChannelAffinityFailure(c) {
return false
}
Expand Down
54 changes: 54 additions & 0 deletions controller/relay_retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package controller

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

func TestShouldRetrySkipsClientCanceledErrors(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
err *types.NewAPIError
}{
{
name: "wrapped context canceled",
err: types.NewErrorWithStatusCode(
fmt.Errorf("request context done: %w", context.Canceled),
types.ErrorCodeBadResponse,
http.StatusInternalServerError,
),
},
{
name: "client gone stream marker",
err: types.NewErrorWithStatusCode(
fmt.Errorf("stream ended: reason=client_gone end_error=%q", context.Canceled.Error()),
types.ErrorCodeBadResponse,
http.StatusInternalServerError,
),
},
{
name: "channel-coded cancellation",
err: types.NewErrorWithStatusCode(
fmt.Errorf("request context done: %w", context.Canceled),
types.ErrorCodeChannelInvalidKey,
http.StatusInternalServerError,
),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())

assert.False(t, shouldRetry(ctx, tt.err, 3))
})
}
}
Loading