From 4c43012e6cfc21907cef44cb30909c6cbfbe359f Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sat, 2 Mar 2024 22:46:26 +0800 Subject: [PATCH] fix: Improve handling of small weights in channel selection logic --- model/cache.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/model/cache.go b/model/cache.go index b1199e2..dc85639 100644 --- a/model/cache.go +++ b/model/cache.go @@ -291,10 +291,13 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error } } } + // 平滑系数 + smoothingFactor := 10 + // Calculate the total weight of all channels up to endIdx totalWeight := 0 for _, channel := range channels[:endIdx] { - totalWeight += channel.GetWeight() + totalWeight += channel.GetWeight() + smoothingFactor } if totalWeight == 0 { @@ -307,8 +310,8 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error // Find a channel based on its weight for _, channel := range channels[:endIdx] { - randomWeight -= channel.GetWeight() - if randomWeight <= 0 { + randomWeight -= channel.GetWeight() + smoothingFactor + if randomWeight < 0 { return channel, nil } }