channel_select.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "errors"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/constant"
  6. "github.com/QuantumNous/new-api/logger"
  7. "github.com/QuantumNous/new-api/model"
  8. "github.com/QuantumNous/new-api/setting"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type RetryParam struct {
  12. Ctx *gin.Context
  13. TokenGroup string
  14. ModelName string
  15. Retry *int
  16. resetNextTry bool
  17. }
  18. func (p *RetryParam) GetRetry() int {
  19. if p.Retry == nil {
  20. return 0
  21. }
  22. return *p.Retry
  23. }
  24. func (p *RetryParam) SetRetry(retry int) {
  25. p.Retry = &retry
  26. }
  27. func (p *RetryParam) IncreaseRetry() {
  28. if p.resetNextTry {
  29. p.resetNextTry = false
  30. return
  31. }
  32. if p.Retry == nil {
  33. p.Retry = new(int)
  34. }
  35. *p.Retry++
  36. }
  37. func (p *RetryParam) ResetRetryNextTry() {
  38. p.resetNextTry = true
  39. }
  40. // CacheGetRandomSatisfiedChannel tries to get a random channel that satisfies the requirements.
  41. // 尝试获取一个满足要求的随机渠道。
  42. //
  43. // For "auto" tokenGroup with cross-group Retry enabled:
  44. // 对于启用了跨分组重试的 "auto" tokenGroup:
  45. //
  46. // - Each group will exhaust all its priorities before moving to the next group.
  47. // 每个分组会用完所有优先级后才会切换到下一个分组。
  48. //
  49. // - Uses ContextKeyAutoGroupIndex to track current group index.
  50. // 使用 ContextKeyAutoGroupIndex 跟踪当前分组索引。
  51. //
  52. // - Uses ContextKeyAutoGroupRetryIndex to track the global Retry count when current group started.
  53. // 使用 ContextKeyAutoGroupRetryIndex 跟踪当前分组开始时的全局重试次数。
  54. //
  55. // - priorityRetry = Retry - startRetryIndex, represents the priority level within current group.
  56. // priorityRetry = Retry - startRetryIndex,表示当前分组内的优先级级别。
  57. //
  58. // - When GetRandomSatisfiedChannel returns nil (priorities exhausted), moves to next group.
  59. // 当 GetRandomSatisfiedChannel 返回 nil(优先级用完)时,切换到下一个分组。
  60. //
  61. // Example flow (2 groups, each with 2 priorities, RetryTimes=3):
  62. // 示例流程(2个分组,每个有2个优先级,RetryTimes=3):
  63. //
  64. // Retry=0: GroupA, priority0 (startRetryIndex=0, priorityRetry=0)
  65. // 分组A, 优先级0
  66. //
  67. // Retry=1: GroupA, priority1 (startRetryIndex=0, priorityRetry=1)
  68. // 分组A, 优先级1
  69. //
  70. // Retry=2: GroupA exhausted → GroupB, priority0 (startRetryIndex=2, priorityRetry=0)
  71. // 分组A用完 → 分组B, 优先级0
  72. //
  73. // Retry=3: GroupB, priority1 (startRetryIndex=2, priorityRetry=1)
  74. // 分组B, 优先级1
  75. func CacheGetRandomSatisfiedChannel(param *RetryParam) (*model.Channel, string, error) {
  76. var channel *model.Channel
  77. var err error
  78. selectGroup := param.TokenGroup
  79. userGroup := common.GetContextKeyString(param.Ctx, constant.ContextKeyUserGroup)
  80. if param.TokenGroup == "auto" {
  81. if len(setting.GetAutoGroups()) == 0 {
  82. return nil, selectGroup, errors.New("auto groups is not enabled")
  83. }
  84. autoGroups := GetUserAutoGroup(userGroup)
  85. // startGroupIndex: the group index to start searching from
  86. // startGroupIndex: 开始搜索的分组索引
  87. startGroupIndex := 0
  88. crossGroupRetry := common.GetContextKeyBool(param.Ctx, constant.ContextKeyTokenCrossGroupRetry)
  89. if lastGroupIndex, exists := common.GetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex); exists {
  90. if idx, ok := lastGroupIndex.(int); ok {
  91. startGroupIndex = idx
  92. }
  93. }
  94. for i := startGroupIndex; i < len(autoGroups); i++ {
  95. autoGroup := autoGroups[i]
  96. // Calculate priorityRetry for current group
  97. // 计算当前分组的 priorityRetry
  98. priorityRetry := param.GetRetry()
  99. // If moved to a new group, reset priorityRetry and update startRetryIndex
  100. // 如果切换到新分组,重置 priorityRetry 并更新 startRetryIndex
  101. if i > startGroupIndex {
  102. priorityRetry = 0
  103. }
  104. logger.LogDebug(param.Ctx, "Auto selecting group: %s, priorityRetry: %d", autoGroup, priorityRetry)
  105. channel, _ = model.GetRandomSatisfiedChannel(autoGroup, param.ModelName, priorityRetry)
  106. if channel == nil {
  107. // Current group has no available channel for this model, try next group
  108. // 当前分组没有该模型的可用渠道,尝试下一个分组
  109. logger.LogDebug(param.Ctx, "No available channel in group %s for model %s at priorityRetry %d, trying next group", autoGroup, param.ModelName, priorityRetry)
  110. // 重置状态以尝试下一个分组
  111. common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex, i+1)
  112. common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupRetryIndex, 0)
  113. // Reset retry counter so outer loop can continue for next group
  114. // 重置重试计数器,以便外层循环可以为下一个分组继续
  115. param.SetRetry(0)
  116. continue
  117. }
  118. common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroup, autoGroup)
  119. selectGroup = autoGroup
  120. logger.LogDebug(param.Ctx, "Auto selected group: %s", autoGroup)
  121. // Prepare state for next retry
  122. // 为下一次重试准备状态
  123. if crossGroupRetry && priorityRetry >= common.RetryTimes {
  124. // Current group has exhausted all retries, prepare to switch to next group
  125. // This request still uses current group, but next retry will use next group
  126. // 当前分组已用完所有重试次数,准备切换到下一个分组
  127. // 本次请求仍使用当前分组,但下次重试将使用下一个分组
  128. logger.LogDebug(param.Ctx, "Current group %s retries exhausted (priorityRetry=%d >= RetryTimes=%d), preparing switch to next group for next retry", autoGroup, priorityRetry, common.RetryTimes)
  129. common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex, i+1)
  130. // Reset retry counter so outer loop can continue for next group
  131. // 重置重试计数器,以便外层循环可以为下一个分组继续
  132. param.SetRetry(0)
  133. param.ResetRetryNextTry()
  134. } else {
  135. // Stay in current group, save current state
  136. // 保持在当前分组,保存当前状态
  137. common.SetContextKey(param.Ctx, constant.ContextKeyAutoGroupIndex, i)
  138. }
  139. break
  140. }
  141. } else {
  142. channel, err = model.GetRandomSatisfiedChannel(param.TokenGroup, param.ModelName, param.GetRetry())
  143. if err != nil {
  144. return nil, param.TokenGroup, err
  145. }
  146. }
  147. return channel, selectGroup, nil
  148. }