This commit introduces a major architectural refactoring to improve quota management, centralize logging, and streamline the relay handling logic. Key changes: - **Pre-consume Quota:** Implements a new mechanism to check and reserve user quota *before* making the request to the upstream provider. This ensures more accurate quota deduction and prevents users from exceeding their limits due to concurrent requests. - **Unified Relay Handlers:** Refactors the relay logic to use generic handlers (e.g., `ChatHandler`, `ImageHandler`) instead of provider-specific implementations. This significantly reduces code duplication and simplifies adding new channels. - **Centralized Logger:** A new dedicated `logger` package is introduced, and all system logging calls are migrated to use it, moving this responsibility out of the `common` package. - **Code Reorganization:** DTOs are generalized (e.g., `dalle.go` -> `openai_image.go`) and utility code is moved to more appropriate packages (e.g., `common/http.go` -> `service/http.go`) for better code structure.
77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package setting
|
||
|
||
import (
|
||
"encoding/json"
|
||
"one-api/logger"
|
||
"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
|
||
}
|
||
return copyUserUsableGroups
|
||
}
|
||
|
||
func UserUsableGroups2JSONString() string {
|
||
userUsableGroupsMutex.RLock()
|
||
defer userUsableGroupsMutex.RUnlock()
|
||
|
||
jsonBytes, err := json.Marshal(userUsableGroups)
|
||
if err != nil {
|
||
logger.SysError("error marshalling user groups: " + err.Error())
|
||
}
|
||
return string(jsonBytes)
|
||
}
|
||
|
||
func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
|
||
userUsableGroupsMutex.Lock()
|
||
defer userUsableGroupsMutex.Unlock()
|
||
|
||
userUsableGroups = make(map[string]string)
|
||
return json.Unmarshal([]byte(jsonStr), &userUsableGroups)
|
||
}
|
||
|
||
func GetUserUsableGroups(userGroup string) map[string]string {
|
||
groupsCopy := GetUserUsableGroupsCopy()
|
||
if userGroup == "" {
|
||
if _, ok := groupsCopy["default"]; !ok {
|
||
groupsCopy["default"] = "default"
|
||
}
|
||
}
|
||
// 如果userGroup不在UserUsableGroups中,返回UserUsableGroups + userGroup
|
||
if _, ok := groupsCopy[userGroup]; !ok {
|
||
groupsCopy[userGroup] = "用户分组"
|
||
}
|
||
// 如果userGroup在UserUsableGroups中,返回UserUsableGroups
|
||
return groupsCopy
|
||
}
|
||
|
||
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
|
||
}
|
||
return groupName
|
||
}
|