mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-18 00:16:37 +08:00
fix: Improve handling of small weights in channel selection logic
This commit is contained in:
parent
a16e949318
commit
4c43012e6c
@ -291,10 +291,13 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 平滑系数
|
||||||
|
smoothingFactor := 10
|
||||||
|
|
||||||
// Calculate the total weight of all channels up to endIdx
|
// Calculate the total weight of all channels up to endIdx
|
||||||
totalWeight := 0
|
totalWeight := 0
|
||||||
for _, channel := range channels[:endIdx] {
|
for _, channel := range channels[:endIdx] {
|
||||||
totalWeight += channel.GetWeight()
|
totalWeight += channel.GetWeight() + smoothingFactor
|
||||||
}
|
}
|
||||||
|
|
||||||
if totalWeight == 0 {
|
if totalWeight == 0 {
|
||||||
@ -307,8 +310,8 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
|
|||||||
|
|
||||||
// Find a channel based on its weight
|
// Find a channel based on its weight
|
||||||
for _, channel := range channels[:endIdx] {
|
for _, channel := range channels[:endIdx] {
|
||||||
randomWeight -= channel.GetWeight()
|
randomWeight -= channel.GetWeight() + smoothingFactor
|
||||||
if randomWeight <= 0 {
|
if randomWeight < 0 {
|
||||||
return channel, nil
|
return channel, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user