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
9be721ee
authored
Apr 07, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add concurrency control to group ratio management with mutexes
parent
69d790b4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
0 deletions
+17
-0
setting/group_ratio.go
+17
-0
No files found.
setting/group_ratio.go
View file @
9be721ee
...
...
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"one-api/common"
"sync"
)
var
groupRatio
=
map
[
string
]
float64
{
...
...
@@ -11,8 +12,12 @@ var groupRatio = map[string]float64{
"vip"
:
1
,
"svip"
:
1
,
}
var
groupRatioMutex
sync
.
RWMutex
func
GetGroupRatioCopy
()
map
[
string
]
float64
{
groupRatioMutex
.
RLock
()
defer
groupRatioMutex
.
RUnlock
()
groupRatioCopy
:=
make
(
map
[
string
]
float64
)
for
k
,
v
:=
range
groupRatio
{
groupRatioCopy
[
k
]
=
v
...
...
@@ -21,11 +26,17 @@ func GetGroupRatioCopy() map[string]float64 {
}
func
ContainsGroupRatio
(
name
string
)
bool
{
groupRatioMutex
.
RLock
()
defer
groupRatioMutex
.
RUnlock
()
_
,
ok
:=
groupRatio
[
name
]
return
ok
}
func
GroupRatio2JSONString
()
string
{
groupRatioMutex
.
RLock
()
defer
groupRatioMutex
.
RUnlock
()
jsonBytes
,
err
:=
json
.
Marshal
(
groupRatio
)
if
err
!=
nil
{
common
.
SysError
(
"error marshalling model ratio: "
+
err
.
Error
())
...
...
@@ -34,11 +45,17 @@ func GroupRatio2JSONString() string {
}
func
UpdateGroupRatioByJSONString
(
jsonStr
string
)
error
{
groupRatioMutex
.
Lock
()
defer
groupRatioMutex
.
Unlock
()
groupRatio
=
make
(
map
[
string
]
float64
)
return
json
.
Unmarshal
([]
byte
(
jsonStr
),
&
groupRatio
)
}
func
GetGroupRatio
(
name
string
)
float64
{
groupRatioMutex
.
RLock
()
defer
groupRatioMutex
.
RUnlock
()
ratio
,
ok
:=
groupRatio
[
name
]
if
!
ok
{
common
.
SysError
(
"group ratio not found: "
+
name
)
...
...
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