From a2da6a9e9033edb426861e236fe0474c080c961e Mon Sep 17 00:00:00 2001 From: CaIon Date: Sat, 13 Dec 2025 18:09:10 +0800 Subject: [PATCH] refactor(channel_select): improve retry logic with reset functionality --- service/channel_select.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/service/channel_select.go b/service/channel_select.go index afaf4f04e..a3710ef8c 100644 --- a/service/channel_select.go +++ b/service/channel_select.go @@ -12,10 +12,11 @@ import ( ) type RetryParam struct { - Ctx *gin.Context - TokenGroup string - ModelName string - Retry *int + Ctx *gin.Context + TokenGroup string + ModelName string + Retry *int + resetNextTry bool } func (p *RetryParam) GetRetry() int { @@ -30,12 +31,20 @@ func (p *RetryParam) SetRetry(retry int) { } func (p *RetryParam) IncreaseRetry() { + if p.resetNextTry { + p.resetNextTry = false + return + } if p.Retry == nil { p.Retry = new(int) } *p.Retry++ } +func (p *RetryParam) ResetRetryNextTry() { + p.resetNextTry = true +} + // CacheGetRandomSatisfiedChannel tries to get a random channel that satisfies the requirements. // 尝试获取一个满足要求的随机渠道。 // @@ -134,7 +143,8 @@ func CacheGetRandomSatisfiedChannel(param *RetryParam) (*model.Channel, string, common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex, i+1) // Reset retry counter so outer loop can continue for next group // 重置重试计数器,以便外层循环可以为下一个分组继续 - param.SetRetry(-1) + param.SetRetry(0) + param.ResetRetryNextTry() } else { // Stay in current group, save current state // 保持在当前分组,保存当前状态