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
ebdab831
authored
Sep 18, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix priority not updated & random choice not working
parent
ead3b915
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
6 deletions
+19
-6
model/ability.go
+1
-1
model/cache.go
+10
-4
model/channel.go
+8
-1
No files found.
model/ability.go
View file @
ebdab831
...
@@ -10,7 +10,7 @@ type Ability struct {
...
@@ -10,7 +10,7 @@ type Ability struct {
Model
string
`json:"model" gorm:"primaryKey;autoIncrement:false"`
Model
string
`json:"model" gorm:"primaryKey;autoIncrement:false"`
ChannelId
int
`json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
ChannelId
int
`json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
Enabled
bool
`json:"enabled"`
Enabled
bool
`json:"enabled"`
Priority
int64
`json:"priority" gorm:"bigint;default:0"`
Priority
*
int64
`json:"priority" gorm:"bigint;default:0"`
}
}
func
GetRandomSatisfiedChannel
(
group
string
,
model
string
)
(
*
Channel
,
error
)
{
func
GetRandomSatisfiedChannel
(
group
string
,
model
string
)
(
*
Channel
,
error
)
{
...
...
model/cache.go
View file @
ebdab831
...
@@ -165,7 +165,7 @@ func InitChannelCache() {
...
@@ -165,7 +165,7 @@ func InitChannelCache() {
for
group
,
model2channels
:=
range
newGroup2model2channels
{
for
group
,
model2channels
:=
range
newGroup2model2channels
{
for
model
,
channels
:=
range
model2channels
{
for
model
,
channels
:=
range
model2channels
{
sort
.
Slice
(
channels
,
func
(
i
,
j
int
)
bool
{
sort
.
Slice
(
channels
,
func
(
i
,
j
int
)
bool
{
return
channels
[
i
]
.
Priority
>
channels
[
j
]
.
Priority
return
channels
[
i
]
.
GetPriority
()
>
channels
[
j
]
.
GetPriority
()
})
})
newGroup2model2channels
[
group
][
model
]
=
channels
newGroup2model2channels
[
group
][
model
]
=
channels
}
}
...
@@ -195,11 +195,17 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
...
@@ -195,11 +195,17 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
if
len
(
channels
)
==
0
{
if
len
(
channels
)
==
0
{
return
nil
,
errors
.
New
(
"channel not found"
)
return
nil
,
errors
.
New
(
"channel not found"
)
}
}
endIdx
:=
len
(
channels
)
// choose by priority
// choose by priority
firstChannel
:=
channels
[
0
]
firstChannel
:=
channels
[
0
]
if
firstChannel
.
Priority
>
0
{
if
firstChannel
.
GetPriority
()
>
0
{
return
firstChannel
,
nil
for
i
:=
range
channels
{
if
channels
[
i
]
.
GetPriority
()
!=
firstChannel
.
GetPriority
()
{
endIdx
=
i
break
}
}
}
}
idx
:=
rand
.
Intn
(
len
(
channels
)
)
idx
:=
rand
.
Intn
(
endIdx
)
return
channels
[
idx
],
nil
return
channels
[
idx
],
nil
}
}
model/channel.go
View file @
ebdab831
...
@@ -23,7 +23,7 @@ type Channel struct {
...
@@ -23,7 +23,7 @@ type Channel struct {
Group
string
`json:"group" gorm:"type:varchar(32);default:'default'"`
Group
string
`json:"group" gorm:"type:varchar(32);default:'default'"`
UsedQuota
int64
`json:"used_quota" gorm:"bigint;default:0"`
UsedQuota
int64
`json:"used_quota" gorm:"bigint;default:0"`
ModelMapping
string
`json:"model_mapping" gorm:"type:varchar(1024);default:''"`
ModelMapping
string
`json:"model_mapping" gorm:"type:varchar(1024);default:''"`
Priority
int64
`json:"priority" gorm:"bigint;default:0"`
Priority
*
int64
`json:"priority" gorm:"bigint;default:0"`
}
}
func
GetAllChannels
(
startIdx
int
,
num
int
,
selectAll
bool
)
([]
*
Channel
,
error
)
{
func
GetAllChannels
(
startIdx
int
,
num
int
,
selectAll
bool
)
([]
*
Channel
,
error
)
{
...
@@ -79,6 +79,13 @@ func BatchInsertChannels(channels []Channel) error {
...
@@ -79,6 +79,13 @@ func BatchInsertChannels(channels []Channel) error {
return
nil
return
nil
}
}
func
(
channel
*
Channel
)
GetPriority
()
int64
{
if
channel
==
nil
{
return
0
}
return
*
channel
.
Priority
}
func
(
channel
*
Channel
)
Insert
()
error
{
func
(
channel
*
Channel
)
Insert
()
error
{
var
err
error
var
err
error
err
=
DB
.
Create
(
channel
)
.
Error
err
=
DB
.
Create
(
channel
)
.
Error
...
...
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