Compare commits

...

2 Commits

Author SHA1 Message Date
creamlike1024
90f1dafb55 fix(channel_cache): 修复总权重小于等于0时的渠道选择逻辑 2025-10-29 19:59:39 +08:00
creamlike1024
cba21eb8c7 fix: 当totalWeight小于等于0时设置为1选择第一个渠道 2025-10-29 19:41:45 +08:00

View File

@@ -166,6 +166,21 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel,
}
// Calculate the total weight of all channels up to endIdx
totalWeight := sumWeight * smoothingFactor
// totalWeight 小于等于0时给每个渠道加100的权重然后进行随机选择
if totalWeight <= 0 {
if len(targetChannels) > 0 {
totalWeight = len(targetChannels) * 100
randomWeight := rand.Intn(totalWeight)
for _, channel := range targetChannels {
randomWeight -= 100
if randomWeight <= 0 {
return channel, nil
}
}
}
return nil, errors.New("no available channels")
}
// Generate a random value in the range [0, totalWeight)
randomWeight := rand.Intn(totalWeight)