From 93edde591b0017a9bf790c8238c05755412632c8 Mon Sep 17 00:00:00 2001 From: creamlike1024 Date: Wed, 29 Oct 2025 19:41:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E5=BD=93totalWeight=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E=E7=AD=89=E4=BA=8E0=E6=97=B6=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=B8=BA1=E9=80=89=E6=8B=A9=E7=AC=AC=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=B8=A0=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/channel_cache.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/model/channel_cache.go b/model/channel_cache.go index 0df14572..ddd1ddde 100644 --- a/model/channel_cache.go +++ b/model/channel_cache.go @@ -166,6 +166,11 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, } // Calculate the total weight of all channels up to endIdx totalWeight := sumWeight * smoothingFactor + + // totalWeight 小于等于0时,设置为1,选择第一个渠道 + if totalWeight <= 0 { + totalWeight = 1 + } // Generate a random value in the range [0, totalWeight) randomWeight := rand.Intn(totalWeight) From 5f8c2ea96236f412ffcde992283df8d4f1092642 Mon Sep 17 00:00:00 2001 From: creamlike1024 Date: Wed, 29 Oct 2025 19:59:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(channel=5Fcache):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=80=BB=E6=9D=83=E9=87=8D=E5=B0=8F=E4=BA=8E=E7=AD=89=E4=BA=8E?= =?UTF-8?q?0=E6=97=B6=E7=9A=84=E6=B8=A0=E9=81=93=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/channel_cache.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/model/channel_cache.go b/model/channel_cache.go index ddd1ddde..41ffdaca 100644 --- a/model/channel_cache.go +++ b/model/channel_cache.go @@ -167,9 +167,19 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, // Calculate the total weight of all channels up to endIdx totalWeight := sumWeight * smoothingFactor - // totalWeight 小于等于0时,设置为1,选择第一个渠道 + // totalWeight 小于等于0时,给每个渠道加100的权重,然后进行随机选择 if totalWeight <= 0 { - totalWeight = 1 + 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)