From c52f5ad99761f704362b32aebca8364470ad7df6 Mon Sep 17 00:00:00 2001 From: ckt1031 <65409152+ckt1031@users.noreply.github.com> Date: Mon, 2 Oct 2023 14:54:03 +0800 Subject: [PATCH] fix: caching --- model/cache.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/model/cache.go b/model/cache.go index 6a0fef0c..3ff9d7c5 100644 --- a/model/cache.go +++ b/model/cache.go @@ -203,25 +203,27 @@ func CacheGetRandomSatisfiedChannel(group string, model string, stream bool) (*C } var filteredChannels []*Channel + for _, channel := range channels { - if (stream && channel.AllowStreaming == common.ChannelAllowStreamEnabled) || (!stream && channel.AllowNonStreaming == common.ChannelAllowNonStreamEnabled) { + isStreaming := stream && channel.AllowStreaming == common.ChannelAllowStreamEnabled + isNotStreaming := !stream && channel.AllowNonStreaming == common.ChannelAllowNonStreamEnabled + + if isStreaming || isNotStreaming { filteredChannels = append(filteredChannels, channel) } } - endIdx := len(channels) - + endIdx := len(filteredChannels) // choose by priority - firstChannel := channels[0] + firstChannel := filteredChannels[0] if firstChannel.GetPriority() > 0 { - for i := range channels { - if channels[i].GetPriority() != firstChannel.GetPriority() { + for i := range filteredChannels { + if filteredChannels[i].GetPriority() != firstChannel.GetPriority() { endIdx = i break } } } - idx := rand.Intn(endIdx) return filteredChannels[idx], nil }