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