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
31 changes: 27 additions & 4 deletions controller/channel-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func testChannel(ctx context.Context, channel *model.Channel, testUserID int, te
if resp != nil {
httpResp = resp.(*http.Response)
if httpResp.StatusCode != http.StatusOK {
err := service.RelayErrorHandler(c.Request.Context(), httpResp, true)
upstreamErr := service.RelayErrorHandler(c.Request.Context(), httpResp, true)
common.SysError(fmt.Sprintf(
"channel test bad response: channel_id=%d name=%s type=%d model=%s endpoint_type=%s status=%d err=%v",
channel.Id,
Expand All @@ -447,12 +447,13 @@ func testChannel(ctx context.Context, channel *model.Channel, testUserID int, te
testModel,
endpointType,
httpResp.StatusCode,
err,
upstreamErr,
))
// newAPIError 保留上游真实状态码,供自动禁用规则(状态码/关键词)判断
return testResult{
context: c,
localErr: err,
newAPIError: types.NewOpenAIError(err, types.ErrorCodeBadResponse, http.StatusInternalServerError),
localErr: upstreamErr,
newAPIError: upstreamErr,
}
}
}
Expand Down Expand Up @@ -867,6 +868,10 @@ func TestChannel(c *gin.Context) {
requestCtx = c.Request.Context()
}
result := testChannel(requestCtx, channel, testUserID, testModel, endpointType, isStream)
// 手动测试失败时与正常转发、定时巡检走同一套错误处理,满足条件即禁用/冷却命中的密钥。
// 必须放在 localErr 分支之前:上游返回错误时 localErr 与 newAPIError 同时非空,
// 提前 return 会让错误处理永远执行不到。
processManualTestChannelError(channel, result)
if result.localErr != nil {
resp := gin.H{
"success": false,
Expand Down Expand Up @@ -899,6 +904,24 @@ func TestChannel(c *gin.Context) {
})
}

// processManualTestChannelError 让手动「测试连接」失败与正常转发、定时巡检走同一套
// 错误处理:先按多 Key 限额模式给命中的密钥设置冷却;未命中且满足自动禁用条件
// (全局开关 + 渠道 AutoBan + 状态码/关键词规则)时禁用该密钥。渠道未启用时不处理。
// 注意上游返回错误时 testResult 的 localErr 与 newAPIError 同时非空,不能用 localErr
// 区分"测试未发出",因此仅以 newAPIError 是否存在为准。
func processManualTestChannelError(channel *model.Channel, result testResult) {
if result.newAPIError == nil {
return
}
if channel.Status != common.ChannelStatusEnabled {
return
}
processChannelError(result.context, *types.NewChannelError(
channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey,
common.GetContextKeyString(result.context, constant.ContextKeyChannelKey),
channel.GetAutoBan()), result.newAPIError)
}

// channelTestSummary records the outcome of one channel test cycle so the
// system task can persist a per-run result for history.
type channelTestSummary struct {
Expand Down
6 changes: 6 additions & 0 deletions controller/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,7 @@ func ManageMultiKeys(c *gin.Context) {

channel.ChannelInfo.MultiKeyStatusList[keyIndex] = 2 // disabled

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand Down Expand Up @@ -1744,6 +1745,7 @@ func ManageMultiKeys(c *gin.Context) {
delete(channel.ChannelInfo.MultiKeyCooldownUntil, keyIndex)
}

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand All @@ -1769,6 +1771,7 @@ func ManageMultiKeys(c *gin.Context) {
channel.ChannelInfo.MultiKeyDisabledReason = make(map[int]string)
channel.ChannelInfo.MultiKeyCooldownUntil = make(map[int]int64)

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand Down Expand Up @@ -1816,6 +1819,7 @@ func ManageMultiKeys(c *gin.Context) {
return
}

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand Down Expand Up @@ -1896,6 +1900,7 @@ func ManageMultiKeys(c *gin.Context) {
channel.ChannelInfo.MultiKeyDisabledTime = newDisabledTime
channel.ChannelInfo.MultiKeyDisabledReason = newDisabledReason

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand Down Expand Up @@ -1964,6 +1969,7 @@ func ManageMultiKeys(c *gin.Context) {
channel.ChannelInfo.MultiKeyDisabledTime = newDisabledTime
channel.ChannelInfo.MultiKeyDisabledReason = newDisabledReason

channel.SyncMultiKeyChannelStatus()
err = channel.Update()
if err != nil {
common.ApiError(c, err)
Expand Down
Loading
Loading