MCPcopy Index your code
hub / github.com/aiprodcoder/MIXAPI / getRandomSatisfiedChannel

Function getRandomSatisfiedChannel

model/channel_cache.go:247–325  ·  view source on GitHub ↗
(group string, model string, retry int)

Source from the content-addressed store, hash-verified

245}
246
247func getRandomSatisfiedChannel(group string, model string, retry int) (*Channel, error) {
248 if strings.HasPrefix(model, "gpt-4-gizmo") {
249 model = "gpt-4-gizmo-*"
250 }
251 if strings.HasPrefix(model, "gpt-4o-gizmo") {
252 model = "gpt-4o-gizmo-*"
253 }
254
255 // if memory cache is disabled, get channel directly from database
256 if !common.MemoryCacheEnabled {
257 return GetRandomSatisfiedChannel(group, model, retry, nil)
258 }
259
260 channelSyncLock.RLock()
261 defer channelSyncLock.RUnlock()
262 channels := group2model2channels[group][model]
263
264 if len(channels) == 0 {
265 return nil, errors.New("channel not found")
266 }
267
268 if len(channels) == 1 {
269 if channel, ok := channelsIDM[channels[0]]; ok {
270 return channel, nil
271 }
272 return nil, fmt.Errorf("数据库一致性错误,渠道# %d 不存在,请联系管理员修复", channels[0])
273 }
274
275 uniquePriorities := make(map[int]bool)
276 for _, channelId := range channels {
277 if channel, ok := channelsIDM[channelId]; ok {
278 uniquePriorities[int(channel.GetPriority())] = true
279 } else {
280 return nil, fmt.Errorf("数据库一致性错误,渠道# %d 不存在,请联系管理员修复", channelId)
281 }
282 }
283 var sortedUniquePriorities []int
284 for priority := range uniquePriorities {
285 sortedUniquePriorities = append(sortedUniquePriorities, priority)
286 }
287 sort.Sort(sort.Reverse(sort.IntSlice(sortedUniquePriorities)))
288
289 if retry >= len(uniquePriorities) {
290 retry = len(uniquePriorities) - 1
291 }
292 targetPriority := int64(sortedUniquePriorities[retry])
293
294 // get the priority for the given retry number
295 var targetChannels []*Channel
296 for _, channelId := range channels {
297 if channel, ok := channelsIDM[channelId]; ok {
298 if channel.GetPriority() == targetPriority {
299 targetChannels = append(targetChannels, channel)
300 }
301 } else {
302 return nil, fmt.Errorf("数据库一致性错误,渠道# %d 不存在,请联系管理员修复", channelId)
303 }
304 }

Callers 1

Calls 3

GetPriorityMethod · 0.80
GetWeightMethod · 0.80

Tested by

no test coverage detected