feat: 优化代码,去除多余注释和修改

This commit is contained in:
tbphp
2025-05-05 11:34:57 +08:00
parent 6c3fb7777e
commit 7e7d6112ca
6 changed files with 663 additions and 341 deletions

View File

@@ -94,11 +94,12 @@ func InitOptionMap() {
common.OptionMap["ModelRequestRateLimitCount"] = strconv.Itoa(setting.ModelRequestRateLimitCount)
common.OptionMap["ModelRequestRateLimitDurationMinutes"] = strconv.Itoa(setting.ModelRequestRateLimitDurationMinutes)
common.OptionMap["ModelRequestRateLimitSuccessCount"] = strconv.Itoa(setting.ModelRequestRateLimitSuccessCount)
jsonBytes, _ := json.Marshal(map[string][2]int{})
common.OptionMap["ModelRequestRateLimitGroup"] = string(jsonBytes)
common.OptionMap["ModelRatio"] = operation_setting.ModelRatio2JSONString()
common.OptionMap["ModelPrice"] = operation_setting.ModelPrice2JSONString()
common.OptionMap["CacheRatio"] = operation_setting.CacheRatio2JSONString()
common.OptionMap["GroupRatio"] = setting.GroupRatio2JSONString()
common.OptionMap[setting.ModelRequestRateLimitGroupKey] = "{}" // 添加用户组速率限制默认值
common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString()
common.OptionMap["CompletionRatio"] = operation_setting.CompletionRatio2JSONString()
common.OptionMap["TopUpLink"] = common.TopUpLink
@@ -153,47 +154,31 @@ func SyncOptions(frequency int) {
}
func UpdateOption(key string, value string) error {
originalValue := value // 保存原始值以备后用
originalValue := value
// Validate and format specific keys before saving
if key == setting.ModelRequestRateLimitGroupKey {
if key == "ModelRequestRateLimitGroup" {
var cfg map[string][2]int
// Validate the JSON structure first using the original value
err := json.Unmarshal([]byte(originalValue), &cfg)
if err != nil {
// 提供更具体的错误信息
return fmt.Errorf("无效的 JSON 格式 for %s: %w", key, err)
}
// TODO: 可以添加更细致的结构验证例如检查数组长度是否为2值是否为非负数等。
// if !isValidModelRequestRateLimitGroupConfig(cfg) {
// return fmt.Errorf("无效的配置值 for %s", key)
// }
// If valid, format the JSON before saving
formattedValueBytes, marshalErr := json.MarshalIndent(cfg, "", " ")
if marshalErr != nil {
// This should ideally not happen if validation passed, but handle defensively
return fmt.Errorf("failed to marshal validated %s config: %w", key, marshalErr)
}
value = string(formattedValueBytes) // Use formatted JSON for saving and memory update
value = string(formattedValueBytes)
}
// Save to database
option := Option{
Key: key,
}
// https://gorm.io/docs/update.html#Save-All-Fields
DB.FirstOrCreate(&option, Option{Key: key})
option.Value = value
// Save is a combination function.
// If save value does not contain primary key, it will execute Create,
// otherwise it will execute Update (with all fields).
if err := DB.Save(&option).Error; err != nil {
return fmt.Errorf("保存选项 %s 到数据库失败: %w", key, err) // 添加错误上下文
return fmt.Errorf("保存选项 %s 到数据库失败: %w", key, err)
}
// Update OptionMap in memory using the potentially formatted value
// updateOptionMap 会处理内存中 setting.ModelRequestRateLimitGroupConfig 的更新
return updateOptionMap(key, value)
}
@@ -370,6 +355,8 @@ func updateOptionMap(key string, value string) (err error) {
setting.ModelRequestRateLimitDurationMinutes, _ = strconv.Atoi(value)
case "ModelRequestRateLimitSuccessCount":
setting.ModelRequestRateLimitSuccessCount, _ = strconv.Atoi(value)
case "ModelRequestRateLimitGroup":
err = setting.UpdateModelRequestRateLimitGroup(value)
case "RetryTimes":
common.RetryTimes, _ = strconv.Atoi(value)
case "DataExportInterval":
@@ -404,15 +391,6 @@ func updateOptionMap(key string, value string) (err error) {
operation_setting.AutomaticDisableKeywordsFromString(value)
case "StreamCacheQueueLength":
setting.StreamCacheQueueLength, _ = strconv.Atoi(value)
case setting.ModelRequestRateLimitGroupKey:
// Use the (potentially formatted) value passed from UpdateOption
// to update the actual configuration in memory.
// This is the single point where the memory state for this specific setting is updated.
err = setting.UpdateModelRequestRateLimitGroupConfig(value)
if err != nil {
// 添加错误上下文
err = fmt.Errorf("更新内存中的 %s 配置失败: %w", key, err)
}
}
return err
}
@@ -440,4 +418,4 @@ func handleConfigUpdate(key, value string) bool {
config.UpdateConfigFromMap(cfg, configMap)
return true // 已处理
}
}