mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 16:06:38 +08:00
feat: 支持未开启缓存下本地重试
This commit is contained in:
parent
257cfc2390
commit
462c328d4b
@ -3,6 +3,7 @@ package model
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -27,8 +28,7 @@ func GetGroupModels(group string) []string {
|
|||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
func getPriority(group string, model string, retry int) (int, error) {
|
||||||
var abilities []Ability
|
|
||||||
groupCol := "`group`"
|
groupCol := "`group`"
|
||||||
trueVal := "1"
|
trueVal := "1"
|
||||||
if common.UsingPostgreSQL {
|
if common.UsingPostgreSQL {
|
||||||
@ -36,9 +36,55 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
|||||||
trueVal = "true"
|
trueVal = "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error = nil
|
var priorities []int
|
||||||
|
err := DB.Model(&Ability{}).
|
||||||
|
Select("DISTINCT(priority)").
|
||||||
|
Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model).
|
||||||
|
Order("priority DESC"). // 按优先级降序排序
|
||||||
|
Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// 处理错误
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确定要使用的优先级
|
||||||
|
var priorityToUse int
|
||||||
|
if retry >= len(priorities) {
|
||||||
|
// 如果重试次数大于优先级数,则使用最小的优先级
|
||||||
|
priorityToUse = priorities[len(priorities)-1]
|
||||||
|
} else {
|
||||||
|
priorityToUse = priorities[retry]
|
||||||
|
}
|
||||||
|
return priorityToUse, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getChannelQuery(group string, model string, retry int) *gorm.DB {
|
||||||
|
groupCol := "`group`"
|
||||||
|
trueVal := "1"
|
||||||
|
if common.UsingPostgreSQL {
|
||||||
|
groupCol = `"group"`
|
||||||
|
trueVal = "true"
|
||||||
|
}
|
||||||
maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model)
|
maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model)
|
||||||
channelQuery := DB.Where(groupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = (?)", group, model, maxPrioritySubQuery)
|
channelQuery := DB.Where(groupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = (?)", group, model, maxPrioritySubQuery)
|
||||||
|
if retry != 0 {
|
||||||
|
priority, err := getPriority(group, model, retry)
|
||||||
|
if err != nil {
|
||||||
|
common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error()))
|
||||||
|
} else {
|
||||||
|
channelQuery = DB.Where(groupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = ?", group, model, priority)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return channelQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, error) {
|
||||||
|
var abilities []Ability
|
||||||
|
|
||||||
|
var err error = nil
|
||||||
|
channelQuery := getChannelQuery(group, model, retry)
|
||||||
if common.UsingSQLite || common.UsingPostgreSQL {
|
if common.UsingSQLite || common.UsingPostgreSQL {
|
||||||
err = channelQuery.Order("weight DESC").Find(&abilities).Error
|
err = channelQuery.Order("weight DESC").Find(&abilities).Error
|
||||||
} else {
|
} else {
|
||||||
|
@ -272,7 +272,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string, retry int) (*Cha
|
|||||||
|
|
||||||
// if memory cache is disabled, get channel directly from database
|
// if memory cache is disabled, get channel directly from database
|
||||||
if !common.MemoryCacheEnabled {
|
if !common.MemoryCacheEnabled {
|
||||||
return GetRandomSatisfiedChannel(group, model)
|
return GetRandomSatisfiedChannel(group, model, retry)
|
||||||
}
|
}
|
||||||
channelSyncLock.RLock()
|
channelSyncLock.RLock()
|
||||||
defer channelSyncLock.RUnlock()
|
defer channelSyncLock.RUnlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user