Commit 2c07014f by KamiPasi Committed by GitHub

🔧 refactor(user_usable_group): enhance concurrency control with mutex for user groups management

parent 4a02c5e2
......@@ -3,14 +3,19 @@ package setting
import (
"encoding/json"
"one-api/common"
"sync"
)
var userUsableGroups = map[string]string{
"default": "默认分组",
"vip": "vip分组",
}
var userUsableGroupsMutex sync.RWMutex
func GetUserUsableGroupsCopy() map[string]string {
userUsableGroupsMutex.RLock()
defer userUsableGroupsMutex.RUnlock()
copyUserUsableGroups := make(map[string]string)
for k, v := range userUsableGroups {
copyUserUsableGroups[k] = v
......@@ -19,6 +24,9 @@ func GetUserUsableGroupsCopy() map[string]string {
}
func UserUsableGroups2JSONString() string {
userUsableGroupsMutex.RLock()
defer userUsableGroupsMutex.RUnlock()
jsonBytes, err := json.Marshal(userUsableGroups)
if err != nil {
common.SysError("error marshalling user groups: " + err.Error())
......@@ -27,6 +35,9 @@ func UserUsableGroups2JSONString() string {
}
func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
userUsableGroupsMutex.Lock()
defer userUsableGroupsMutex.Unlock()
userUsableGroups = make(map[string]string)
return json.Unmarshal([]byte(jsonStr), &userUsableGroups)
}
......@@ -47,11 +58,17 @@ func GetUserUsableGroups(userGroup string) map[string]string {
}
func GroupInUserUsableGroups(groupName string) bool {
userUsableGroupsMutex.RLock()
defer userUsableGroupsMutex.RUnlock()
_, ok := userUsableGroups[groupName]
return ok
}
func GetUsableGroupDescription(groupName string) string {
userUsableGroupsMutex.RLock()
defer userUsableGroupsMutex.RUnlock()
if desc, ok := userUsableGroups[groupName]; ok {
return desc
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment