Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b09337e6
authored
Mar 18, 2026
by
Seefs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: honor channel affinity skip-retry when preferred channel is disabled
parent
8ed2ea6e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
6 deletions
+74
-6
middleware/distributor.go
+7
-2
service/channel_affinity.go
+7
-4
service/channel_affinity_template_test.go
+60
-0
No files found.
middleware/distributor.go
View file @
b09337e6
...
...
@@ -101,8 +101,13 @@ func Distribute() func(c *gin.Context) {
if
preferredChannelID
,
found
:=
service
.
GetPreferredChannelByAffinity
(
c
,
modelRequest
.
Model
,
usingGroup
);
found
{
preferred
,
err
:=
model
.
CacheGetChannel
(
preferredChannelID
)
if
err
==
nil
&&
preferred
!=
nil
&&
preferred
.
Status
==
common
.
ChannelStatusEnabled
{
if
usingGroup
==
"auto"
{
if
err
==
nil
&&
preferred
!=
nil
{
if
preferred
.
Status
!=
common
.
ChannelStatusEnabled
{
if
service
.
ShouldSkipRetryAfterChannelAffinityFailure
(
c
)
{
abortWithOpenAiMessage
(
c
,
http
.
StatusForbidden
,
i18n
.
T
(
c
,
i18n
.
MsgDistributorChannelDisabled
))
return
}
}
else
if
usingGroup
==
"auto"
{
userGroup
:=
common
.
GetContextKeyString
(
c
,
constant
.
ContextKeyUserGroup
)
autoGroups
:=
service
.
GetUserAutoGroup
(
userGroup
)
for
_
,
g
:=
range
autoGroups
{
...
...
service/channel_affinity.go
View file @
b09337e6
...
...
@@ -610,14 +610,17 @@ func ShouldSkipRetryAfterChannelAffinityFailure(c *gin.Context) bool {
return
false
}
v
,
ok
:=
c
.
Get
(
ginKeyChannelAffinitySkipRetry
)
if
!
ok
{
return
false
if
ok
{
b
,
ok
:=
v
.
(
bool
)
if
ok
{
return
b
}
}
b
,
ok
:=
v
.
(
bool
)
meta
,
ok
:=
getChannelAffinityMeta
(
c
)
if
!
ok
{
return
false
}
return
b
return
meta
.
SkipRetry
}
func
MarkChannelAffinityUsed
(
c
*
gin
.
Context
,
selectedGroup
string
,
channelID
int
)
{
...
...
service/channel_affinity_template_test.go
View file @
b09337e6
...
...
@@ -116,6 +116,66 @@ func TestApplyChannelAffinityOverrideTemplate_MergeOperations(t *testing.T) {
require
.
Equal
(
t
,
"trim_prefix"
,
secondOp
[
"mode"
])
}
func
TestShouldSkipRetryAfterChannelAffinityFailure
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
ctx
func
()
*
gin
.
Context
want
bool
}{
{
name
:
"nil context"
,
ctx
:
func
()
*
gin
.
Context
{
return
nil
},
want
:
false
,
},
{
name
:
"explicit skip retry flag in context"
,
ctx
:
func
()
*
gin
.
Context
{
ctx
:=
buildChannelAffinityTemplateContextForTest
(
channelAffinityMeta
{
RuleName
:
"rule-explicit-flag"
,
SkipRetry
:
false
,
UsingGroup
:
"default"
,
ModelName
:
"gpt-5"
,
})
ctx
.
Set
(
ginKeyChannelAffinitySkipRetry
,
true
)
return
ctx
},
want
:
true
,
},
{
name
:
"fallback to matched rule meta"
,
ctx
:
func
()
*
gin
.
Context
{
return
buildChannelAffinityTemplateContextForTest
(
channelAffinityMeta
{
RuleName
:
"rule-skip-retry"
,
SkipRetry
:
true
,
UsingGroup
:
"default"
,
ModelName
:
"gpt-5"
,
})
},
want
:
true
,
},
{
name
:
"no flag and no skip retry meta"
,
ctx
:
func
()
*
gin
.
Context
{
return
buildChannelAffinityTemplateContextForTest
(
channelAffinityMeta
{
RuleName
:
"rule-no-skip-retry"
,
SkipRetry
:
false
,
UsingGroup
:
"default"
,
ModelName
:
"gpt-5"
,
})
},
want
:
false
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
tt
.
want
,
ShouldSkipRetryAfterChannelAffinityFailure
(
tt
.
ctx
()))
})
}
}
func
TestChannelAffinityHitCodexTemplatePassHeadersEffective
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment