feat: switch priority when retry (close #1048)
This commit is contained in:
parent
9d8967f7d3
commit
2df877a352
8
common/random.go
Normal file
8
common/random.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
|
// RandRange returns a random number between min and max (max is not included)
|
||||||
|
func RandRange(min, max int) int {
|
||||||
|
return min + rand.Intn(max-min)
|
||||||
|
}
|
@ -62,7 +62,7 @@ func Relay(c *gin.Context) {
|
|||||||
retryTimes = 0
|
retryTimes = 0
|
||||||
}
|
}
|
||||||
for i := retryTimes; i > 0; i-- {
|
for i := retryTimes; i > 0; i-- {
|
||||||
channel, err := dbmodel.CacheGetRandomSatisfiedChannel(group, originalModel)
|
channel, err := dbmodel.CacheGetRandomSatisfiedChannel(group, originalModel, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "CacheGetRandomSatisfiedChannel failed: %w", err)
|
logger.Errorf(ctx, "CacheGetRandomSatisfiedChannel failed: %w", err)
|
||||||
break
|
break
|
||||||
|
@ -68,7 +68,7 @@ func Distribute() func(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestModel = modelRequest.Model
|
requestModel = modelRequest.Model
|
||||||
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
|
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
|
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
|
||||||
if channel != nil {
|
if channel != nil {
|
||||||
|
@ -191,7 +191,7 @@ func SyncChannelCache(frequency int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
func CacheGetRandomSatisfiedChannel(group string, model string, ignoreFirstPriority bool) (*Channel, error) {
|
||||||
if !config.MemoryCacheEnabled {
|
if !config.MemoryCacheEnabled {
|
||||||
return GetRandomSatisfiedChannel(group, model)
|
return GetRandomSatisfiedChannel(group, model)
|
||||||
}
|
}
|
||||||
@ -213,5 +213,10 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
idx := rand.Intn(endIdx)
|
idx := rand.Intn(endIdx)
|
||||||
|
if ignoreFirstPriority {
|
||||||
|
if endIdx < len(channels) { // which means there are more than one priority
|
||||||
|
idx = common.RandRange(endIdx, len(channels))
|
||||||
|
}
|
||||||
|
}
|
||||||
return channels[idx], nil
|
return channels[idx], nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user