Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
37ec213d
authored
Apr 06, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 支持未开启缓存下本地重试
parent
203b97e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
4 deletions
+50
-4
model/ability.go
+49
-3
model/cache.go
+1
-1
No files found.
model/ability.go
View file @
37ec213d
...
...
@@ -3,6 +3,7 @@ package model
import
(
"errors"
"fmt"
"gorm.io/gorm"
"one-api/common"
"strings"
)
...
...
@@ -27,8 +28,7 @@ func GetGroupModels(group string) []string {
return
models
}
func
GetRandomSatisfiedChannel
(
group
string
,
model
string
)
(
*
Channel
,
error
)
{
var
abilities
[]
Ability
func
getPriority
(
group
string
,
model
string
,
retry
int
)
(
int
,
error
)
{
groupCol
:=
"`group`"
trueVal
:=
"1"
if
common
.
UsingPostgreSQL
{
...
...
@@ -36,9 +36,55 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
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
)
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
{
err
=
channelQuery
.
Order
(
"weight DESC"
)
.
Find
(
&
abilities
)
.
Error
}
else
{
...
...
model/cache.go
View file @
37ec213d
...
...
@@ -272,7 +272,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string, retry int) (*Cha
// if memory cache is disabled, get channel directly from database
if
!
common
.
MemoryCacheEnabled
{
return
GetRandomSatisfiedChannel
(
group
,
model
)
return
GetRandomSatisfiedChannel
(
group
,
model
,
retry
)
}
channelSyncLock
.
RLock
()
defer
channelSyncLock
.
RUnlock
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment