@@ -3,29 +3,27 @@ package ratio_setting
33import (
44 "encoding/json"
55 "errors"
6- "sync"
76
87 "github.com/QuantumNous/new-api/common"
98 "github.com/QuantumNous/new-api/setting/config"
109 "github.com/QuantumNous/new-api/types"
1110)
1211
13- var groupRatio = map [string ]float64 {
12+ var defaultGroupRatio = map [string ]float64 {
1413 "default" : 1 ,
1514 "vip" : 1 ,
1615 "svip" : 1 ,
1716}
1817
19- var groupRatioMutex sync. RWMutex
18+ var groupRatioMap = types . NewRWMap [ string , float64 ]()
2019
21- var (
22- GroupGroupRatio = map [string ]map [string ]float64 {
23- "vip" : {
24- "edit_this" : 0.9 ,
25- },
26- }
27- groupGroupRatioMutex sync.RWMutex
28- )
20+ var defaultGroupGroupRatio = map [string ]map [string ]float64 {
21+ "vip" : {
22+ "edit_this" : 0.9 ,
23+ },
24+ }
25+
26+ var groupGroupRatioMap = types .NewRWMap [string , map [string ]float64 ]()
2927
3028var defaultGroupSpecialUsableGroup = map [string ]map [string ]string {
3129 "vip" : {
@@ -35,9 +33,9 @@ var defaultGroupSpecialUsableGroup = map[string]map[string]string{
3533}
3634
3735type GroupRatioSetting struct {
38- GroupRatio map [string ] float64 `json:"group_ratio"`
39- GroupGroupRatio map [string ] map [string ]float64 `json:"group_group_ratio"`
40- GroupSpecialUsableGroup * types.RWMap [string , map [string ]string ] `json:"group_special_usable_group"`
36+ GroupRatio * types. RWMap [string , float64 ] `json:"group_ratio"`
37+ GroupGroupRatio * types. RWMap [string , map [string ]float64 ] `json:"group_group_ratio"`
38+ GroupSpecialUsableGroup * types.RWMap [string , map [string ]string ] `json:"group_special_usable_group"`
4139}
4240
4341var groupRatioSetting GroupRatioSetting
@@ -46,10 +44,13 @@ func init() {
4644 groupSpecialUsableGroup := types .NewRWMap [string , map [string ]string ]()
4745 groupSpecialUsableGroup .AddAll (defaultGroupSpecialUsableGroup )
4846
47+ groupRatioMap .AddAll (defaultGroupRatio )
48+ groupGroupRatioMap .AddAll (defaultGroupGroupRatio )
49+
4950 groupRatioSetting = GroupRatioSetting {
5051 GroupSpecialUsableGroup : groupSpecialUsableGroup ,
51- GroupRatio : groupRatio ,
52- GroupGroupRatio : GroupGroupRatio ,
52+ GroupRatio : groupRatioMap ,
53+ GroupGroupRatio : groupGroupRatioMap ,
5354 }
5455
5556 config .GlobalConfig .Register ("group_ratio_setting" , & groupRatioSetting )
@@ -64,48 +65,24 @@ func GetGroupRatioSetting() *GroupRatioSetting {
6465}
6566
6667func GetGroupRatioCopy () map [string ]float64 {
67- groupRatioMutex .RLock ()
68- defer groupRatioMutex .RUnlock ()
69-
70- groupRatioCopy := make (map [string ]float64 )
71- for k , v := range groupRatio {
72- groupRatioCopy [k ] = v
73- }
74- return groupRatioCopy
68+ return groupRatioMap .ReadAll ()
7569}
7670
7771func ContainsGroupRatio (name string ) bool {
78- groupRatioMutex .RLock ()
79- defer groupRatioMutex .RUnlock ()
80-
81- _ , ok := groupRatio [name ]
72+ _ , ok := groupRatioMap .Get (name )
8273 return ok
8374}
8475
8576func GroupRatio2JSONString () string {
86- groupRatioMutex .RLock ()
87- defer groupRatioMutex .RUnlock ()
88-
89- jsonBytes , err := json .Marshal (groupRatio )
90- if err != nil {
91- common .SysLog ("error marshalling model ratio: " + err .Error ())
92- }
93- return string (jsonBytes )
77+ return groupRatioMap .MarshalJSONString ()
9478}
9579
9680func UpdateGroupRatioByJSONString (jsonStr string ) error {
97- groupRatioMutex .Lock ()
98- defer groupRatioMutex .Unlock ()
99-
100- groupRatio = make (map [string ]float64 )
101- return json .Unmarshal ([]byte (jsonStr ), & groupRatio )
81+ return types .LoadFromJsonString (groupRatioMap , jsonStr )
10282}
10383
10484func GetGroupRatio (name string ) float64 {
105- groupRatioMutex .RLock ()
106- defer groupRatioMutex .RUnlock ()
107-
108- ratio , ok := groupRatio [name ]
85+ ratio , ok := groupRatioMap .Get (name )
10986 if ! ok {
11087 common .SysLog ("group ratio not found: " + name )
11188 return 1
@@ -114,10 +91,7 @@ func GetGroupRatio(name string) float64 {
11491}
11592
11693func GetGroupGroupRatio (userGroup , usingGroup string ) (float64 , bool ) {
117- groupGroupRatioMutex .RLock ()
118- defer groupGroupRatioMutex .RUnlock ()
119-
120- gp , ok := GroupGroupRatio [userGroup ]
94+ gp , ok := groupGroupRatioMap .Get (userGroup )
12195 if ! ok {
12296 return - 1 , false
12397 }
@@ -129,22 +103,11 @@ func GetGroupGroupRatio(userGroup, usingGroup string) (float64, bool) {
129103}
130104
131105func GroupGroupRatio2JSONString () string {
132- groupGroupRatioMutex .RLock ()
133- defer groupGroupRatioMutex .RUnlock ()
134-
135- jsonBytes , err := json .Marshal (GroupGroupRatio )
136- if err != nil {
137- common .SysLog ("error marshalling group-group ratio: " + err .Error ())
138- }
139- return string (jsonBytes )
106+ return groupGroupRatioMap .MarshalJSONString ()
140107}
141108
142109func UpdateGroupGroupRatioByJSONString (jsonStr string ) error {
143- groupGroupRatioMutex .Lock ()
144- defer groupGroupRatioMutex .Unlock ()
145-
146- GroupGroupRatio = make (map [string ]map [string ]float64 )
147- return json .Unmarshal ([]byte (jsonStr ), & GroupGroupRatio )
110+ return types .LoadFromJsonString (groupGroupRatioMap , jsonStr )
148111}
149112
150113func CheckGroupRatio (jsonStr string ) error {
0 commit comments