mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 09:16:36 +08:00
fix: add read/write locks for ModelRatio and GroupRatio to prevent concurrent map read/write issues (#2067)
This commit is contained in:
parent
07808122a6
commit
3e3b8230ac
@ -3,8 +3,10 @@ package ratio
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var groupRatioLock sync.RWMutex
|
||||||
var GroupRatio = map[string]float64{
|
var GroupRatio = map[string]float64{
|
||||||
"default": 1,
|
"default": 1,
|
||||||
"vip": 1,
|
"vip": 1,
|
||||||
@ -20,11 +22,15 @@ func GroupRatio2JSONString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateGroupRatioByJSONString(jsonStr string) error {
|
func UpdateGroupRatioByJSONString(jsonStr string) error {
|
||||||
|
groupRatioLock.Lock()
|
||||||
|
defer groupRatioLock.Unlock()
|
||||||
GroupRatio = make(map[string]float64)
|
GroupRatio = make(map[string]float64)
|
||||||
return json.Unmarshal([]byte(jsonStr), &GroupRatio)
|
return json.Unmarshal([]byte(jsonStr), &GroupRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroupRatio(name string) float64 {
|
func GetGroupRatio(name string) float64 {
|
||||||
|
groupRatioLock.RLock()
|
||||||
|
defer groupRatioLock.RUnlock()
|
||||||
ratio, ok := GroupRatio[name]
|
ratio, ok := GroupRatio[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.SysError("group ratio not found: " + name)
|
logger.SysError("group ratio not found: " + name)
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
)
|
)
|
||||||
@ -15,6 +16,8 @@ const (
|
|||||||
RMB = USD / USD2RMB
|
RMB = USD / USD2RMB
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var modelRatioLock sync.RWMutex
|
||||||
|
|
||||||
// ModelRatio
|
// ModelRatio
|
||||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
||||||
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
|
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
|
||||||
@ -417,11 +420,15 @@ func ModelRatio2JSONString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateModelRatioByJSONString(jsonStr string) error {
|
func UpdateModelRatioByJSONString(jsonStr string) error {
|
||||||
|
modelRatioLock.Lock()
|
||||||
|
defer modelRatioLock.Unlock()
|
||||||
ModelRatio = make(map[string]float64)
|
ModelRatio = make(map[string]float64)
|
||||||
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetModelRatio(name string, channelType int) float64 {
|
func GetModelRatio(name string, channelType int) float64 {
|
||||||
|
modelRatioLock.RLock()
|
||||||
|
defer modelRatioLock.RUnlock()
|
||||||
if strings.HasPrefix(name, "qwen-") && strings.HasSuffix(name, "-internet") {
|
if strings.HasPrefix(name, "qwen-") && strings.HasSuffix(name, "-internet") {
|
||||||
name = strings.TrimSuffix(name, "-internet")
|
name = strings.TrimSuffix(name, "-internet")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user