fix(channel_cache): 修复总权重小于等于0时的渠道选择逻辑
This commit is contained in:
parent
93edde591b
commit
5f8c2ea962
@ -167,9 +167,19 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel,
|
|||||||
// Calculate the total weight of all channels up to endIdx
|
// Calculate the total weight of all channels up to endIdx
|
||||||
totalWeight := sumWeight * smoothingFactor
|
totalWeight := sumWeight * smoothingFactor
|
||||||
|
|
||||||
// totalWeight 小于等于0时,设置为1,选择第一个渠道
|
// totalWeight 小于等于0时,给每个渠道加100的权重,然后进行随机选择
|
||||||
if totalWeight <= 0 {
|
if totalWeight <= 0 {
|
||||||
totalWeight = 1
|
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)
|
// Generate a random value in the range [0, totalWeight)
|
||||||
randomWeight := rand.Intn(totalWeight)
|
randomWeight := rand.Intn(totalWeight)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user