Преглед изворни кода

refactor(channel_cache): improve channel selection logic by handling zero total weight and removing unnecessary variable

CaIon пре 4 месеци
родитељ
комит
b2de5e229c
2 измењених фајлова са 15 додато и 24 уклоњено
  1. 0 4
      common/utils.go
  2. 15 20
      model/channel_cache.go

+ 0 - 4
common/utils.go

@@ -230,10 +230,6 @@ func GetUUID() string {
 
 const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
-func init() {
-	rand.New(rand.NewSource(time.Now().UnixNano()))
-}
-
 func GenerateRandomCharsKey(length int) (string, error) {
 	b := make([]byte, length)
 	maxI := big.NewInt(int64(len(keyChars)))

+ 15 - 20
model/channel_cache.go

@@ -142,7 +142,6 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel,
 	targetPriority := int64(sortedUniquePriorities[retry])
 
 	// get the priority for the given retry number
-	var shouldSmooth = false
 	var sumWeight = 0
 	var targetChannels []*Channel
 	for _, channelId := range channels {
@@ -155,38 +154,34 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel,
 			return nil, fmt.Errorf("数据库一致性错误,渠道# %d 不存在,请联系管理员修复", channelId)
 		}
 	}
-	if sumWeight/len(targetChannels) < 10 {
-		shouldSmooth = true
+
+	if len(targetChannels) == 0 {
+		return nil, errors.New(fmt.Sprintf("no channel found, group: %s, model: %s, priority: %d", group, model, targetPriority))
 	}
 
-	// 平滑系数
+	// smoothing factor and adjustment
 	smoothingFactor := 1
-	if shouldSmooth {
+	smoothingAdjustment := 0
+
+	if sumWeight == 0 {
+		// when all channels have weight 0, set sumWeight to the number of channels and set smoothing adjustment to 100
+		// each channel's effective weight = 100
+		sumWeight = len(targetChannels) * 100
+		smoothingAdjustment = 100
+	} else if sumWeight/len(targetChannels) < 10 {
+		// when the average weight is less than 10, set smoothing factor to 100
 		smoothingFactor = 100
 	}
+
 	// Calculate the total weight of all channels up to endIdx
 	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)
 	randomWeight := rand.Intn(totalWeight)
 
 	// Find a channel based on its weight
 	for _, channel := range targetChannels {
-		randomWeight -= channel.GetWeight() * smoothingFactor
+		randomWeight -= channel.GetWeight()*smoothingFactor + smoothingAdjustment
 		if randomWeight < 0 {
 			return channel, nil
 		}