Commit 77da9628 by Seefs Committed by GitHub

Merge pull request #2126 from QuantumNous/fix-randomWeight-panic

fix: 当totalWeight小于等于0时设置为1选择第一个渠道
parents b26333b2 5f8c2ea9
...@@ -166,6 +166,21 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, ...@@ -166,6 +166,21 @@ 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时,给每个渠道加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) // Generate a random value in the range [0, totalWeight)
randomWeight := rand.Intn(totalWeight) randomWeight := rand.Intn(totalWeight)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment