Explorar o código

fix: Improve handling of small weights in channel selection logic

1808837298@qq.com %!s(int64=2) %!d(string=hai) anos
pai
achega
4c43012e6c
Modificáronse 1 ficheiros con 6 adicións e 3 borrados
  1. 6 3
      model/cache.go

+ 6 - 3
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
 		}
 	}