mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-19 09:38:39 +00:00
refactor(channel_select): improve retry logic with reset functionality
This commit is contained in:
@@ -12,10 +12,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RetryParam struct {
|
type RetryParam struct {
|
||||||
Ctx *gin.Context
|
Ctx *gin.Context
|
||||||
TokenGroup string
|
TokenGroup string
|
||||||
ModelName string
|
ModelName string
|
||||||
Retry *int
|
Retry *int
|
||||||
|
resetNextTry bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RetryParam) GetRetry() int {
|
func (p *RetryParam) GetRetry() int {
|
||||||
@@ -30,12 +31,20 @@ func (p *RetryParam) SetRetry(retry int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *RetryParam) IncreaseRetry() {
|
func (p *RetryParam) IncreaseRetry() {
|
||||||
|
if p.resetNextTry {
|
||||||
|
p.resetNextTry = false
|
||||||
|
return
|
||||||
|
}
|
||||||
if p.Retry == nil {
|
if p.Retry == nil {
|
||||||
p.Retry = new(int)
|
p.Retry = new(int)
|
||||||
}
|
}
|
||||||
*p.Retry++
|
*p.Retry++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *RetryParam) ResetRetryNextTry() {
|
||||||
|
p.resetNextTry = true
|
||||||
|
}
|
||||||
|
|
||||||
// CacheGetRandomSatisfiedChannel tries to get a random channel that satisfies the requirements.
|
// 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)
|
common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex, i+1)
|
||||||
// Reset retry counter so outer loop can continue for next group
|
// Reset retry counter so outer loop can continue for next group
|
||||||
// 重置重试计数器,以便外层循环可以为下一个分组继续
|
// 重置重试计数器,以便外层循环可以为下一个分组继续
|
||||||
param.SetRetry(-1)
|
param.SetRetry(0)
|
||||||
|
param.ResetRetryNextTry()
|
||||||
} else {
|
} else {
|
||||||
// Stay in current group, save current state
|
// Stay in current group, save current state
|
||||||
// 保持在当前分组,保存当前状态
|
// 保持在当前分组,保存当前状态
|
||||||
|
|||||||
Reference in New Issue
Block a user