package setting import ( "encoding/json" "one-api/common" "sync" ) var ModelRequestRateLimitEnabled = false var ModelRequestRateLimitDurationMinutes = 1 var ModelRequestRateLimitCount = 0 var ModelRequestRateLimitSuccessCount = 1000 var ModelRequestRateLimitGroup = map[string][2]int{} var ModelRequestRateLimitMutex sync.RWMutex func ModelRequestRateLimitGroup2JSONString() string { ModelRequestRateLimitMutex.RLock() defer ModelRequestRateLimitMutex.RUnlock() jsonBytes, err := json.Marshal(ModelRequestRateLimitGroup) if err != nil { common.SysError("error marshalling model ratio: " + err.Error()) } return string(jsonBytes) } func UpdateModelRequestRateLimitGroupByJSONString(jsonStr string) error { ModelRequestRateLimitMutex.RLock() defer ModelRequestRateLimitMutex.RUnlock() ModelRequestRateLimitGroup = make(map[string][2]int) return json.Unmarshal([]byte(jsonStr), &ModelRequestRateLimitGroup) } func GetGroupRateLimit(group string) (totalCount, successCount int, found bool) { ModelRequestRateLimitMutex.RLock() defer ModelRequestRateLimitMutex.RUnlock() if ModelRequestRateLimitGroup == nil { return 0, 0, false } limits, found := ModelRequestRateLimitGroup[group] if !found { return 0, 0, false } return limits[0], limits[1], true }