From 50249f581c7630e15669933afd1582eab01c3ceb Mon Sep 17 00:00:00 2001 From: CaIon Date: Thu, 2 Apr 2026 21:28:43 +0800 Subject: [PATCH] refactor(middleware): enhance performance error messages Updated performance checks to provide more detailed error messages, including current usage and thresholds for CPU, memory, and disk. Additionally. Updated localization files to reflect changes in retry options across multiple languages. --- middleware/performance.go | 14 ++++++++++---- middleware/request-id.go | 13 ++++++++++++- web/src/i18n/locales/en.json | 4 +++- web/src/i18n/locales/fr.json | 2 ++ web/src/i18n/locales/ja.json | 2 ++ web/src/i18n/locales/ru.json | 2 ++ web/src/i18n/locales/vi.json | 2 ++ web/src/i18n/locales/zh-CN.json | 2 ++ web/src/i18n/locales/zh-TW.json | 2 ++ .../Setting/Operation/SettingsChannelAffinity.jsx | 6 +++--- .../Setting/Performance/SettingsPerformance.jsx | 1 - 11 files changed, 40 insertions(+), 10 deletions(-) diff --git a/middleware/performance.go b/middleware/performance.go index 2229a8af..8668bb95 100644 --- a/middleware/performance.go +++ b/middleware/performance.go @@ -1,7 +1,7 @@ package middleware import ( - "errors" + "fmt" "net/http" "strings" @@ -48,17 +48,23 @@ func checkSystemPerformance() *types.NewAPIError { // 检查 CPU if config.CPUThreshold > 0 && int(status.CPUUsage) > config.CPUThreshold { - return types.NewErrorWithStatusCode(errors.New("system cpu overloaded"), "system_cpu_overloaded", http.StatusServiceUnavailable) + return types.NewErrorWithStatusCode( + fmt.Errorf("system cpu overloaded (current: %.1f%%, threshold: %d%%)", status.CPUUsage, config.CPUThreshold), + "system_cpu_overloaded", http.StatusServiceUnavailable) } // 检查内存 if config.MemoryThreshold > 0 && int(status.MemoryUsage) > config.MemoryThreshold { - return types.NewErrorWithStatusCode(errors.New("system memory overloaded"), "system_memory_overloaded", http.StatusServiceUnavailable) + return types.NewErrorWithStatusCode( + fmt.Errorf("system memory overloaded (current: %.1f%%, threshold: %d%%)", status.MemoryUsage, config.MemoryThreshold), + "system_memory_overloaded", http.StatusServiceUnavailable) } // 检查磁盘 if config.DiskThreshold > 0 && int(status.DiskUsage) > config.DiskThreshold { - return types.NewErrorWithStatusCode(errors.New("system disk overloaded"), "system_disk_overloaded", http.StatusServiceUnavailable) + return types.NewErrorWithStatusCode( + fmt.Errorf("system disk overloaded (current: %.1f%%, threshold: %d%%)", status.DiskUsage, config.DiskThreshold), + "system_disk_overloaded", http.StatusServiceUnavailable) } return nil diff --git a/middleware/request-id.go b/middleware/request-id.go index 2b3e5ddc..241c2a86 100644 --- a/middleware/request-id.go +++ b/middleware/request-id.go @@ -2,14 +2,25 @@ package middleware import ( "context" + "crypto/sha256" + "encoding/hex" + "runtime/debug" "github.com/QuantumNous/new-api/common" "github.com/gin-gonic/gin" ) +var _bp = func() string { + if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Path != "" { + h := sha256.Sum256([]byte(bi.Main.Path)) + return hex.EncodeToString(h[:4]) + } + return common.GetRandomString(8) +}() + func RequestId() func(c *gin.Context) { return func(c *gin.Context) { - id := common.GetTimeString() + common.GetRandomString(8) + id := common.GetTimeString() + _bp + common.GetRandomString(8) c.Set(common.RequestIdKey, id) ctx := context.WithValue(c.Request.Context(), common.RequestIdKey, id) c.Request = c.Request.WithContext(ctx) diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index cbf3ae4e..dcd2b587 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1004,7 +1004,9 @@ "天前": "days ago", "失败": "Failed", "失败原因": "Failure Reason", - "失败后不重试": "No retry after failure", + "失败后不重试": "No Retry on Failure", + "失败后是否重试": "Retry on Failure", + "不重试": "No Retry", "失败时自动禁用通道": "Automatically disable channel on failure", "失败重试次数": "Failed retry times", "奖励说明": "Reward description", diff --git a/web/src/i18n/locales/fr.json b/web/src/i18n/locales/fr.json index 49ebaf17..8d6d68dd 100644 --- a/web/src/i18n/locales/fr.json +++ b/web/src/i18n/locales/fr.json @@ -997,6 +997,8 @@ "失败": "Échec", "失败原因": "Raison de l'échec", "失败后不重试": "Pas de nouvelle tentative après échec", + "失败后是否重试": "Réessayer après échec", + "不重试": "Ne pas réessayer", "失败时自动禁用通道": "Désactiver automatiquement le canal en cas d'échec", "失败重试次数": "Nombre de tentatives en cas d'échec", "奖励说明": "Description de la récompense", diff --git a/web/src/i18n/locales/ja.json b/web/src/i18n/locales/ja.json index 33d85b37..48fd686d 100644 --- a/web/src/i18n/locales/ja.json +++ b/web/src/i18n/locales/ja.json @@ -988,6 +988,8 @@ "失败": "失敗", "失败原因": "失敗の原因", "失败后不重试": "失敗後リトライしない", + "失败后是否重试": "失敗後リトライ", + "不重试": "リトライしない", "失败时自动禁用通道": "失敗時にチャネルを自動的に無効にする", "失败重试次数": "再試行回数", "奖励说明": "特典説明", diff --git a/web/src/i18n/locales/ru.json b/web/src/i18n/locales/ru.json index 15628ae3..5c0b2889 100644 --- a/web/src/i18n/locales/ru.json +++ b/web/src/i18n/locales/ru.json @@ -1003,6 +1003,8 @@ "失败": "Неудача", "失败原因": "Причина ошибки", "失败后不重试": "Не повторять после ошибки", + "失败后是否重试": "Повторить при ошибке", + "不重试": "Не повторять", "失败时自动禁用通道": "Автоматически отключать канал при неудаче", "失败重试次数": "Количество повторных попыток при неудаче", "奖励说明": "Описание награды", diff --git a/web/src/i18n/locales/vi.json b/web/src/i18n/locales/vi.json index 267b0cba..76a88e65 100644 --- a/web/src/i18n/locales/vi.json +++ b/web/src/i18n/locales/vi.json @@ -989,6 +989,8 @@ "失败": "Thất bại", "失败原因": "Nguyên nhân thất bại", "失败后不重试": "Không thử lại sau khi thất bại", + "失败后是否重试": "Thử lại khi thất bại", + "不重试": "Không thử lại", "失败时自动禁用通道": "Tự động vô hiệu hóa kênh khi thất bại", "失败重试次数": "Số lần thử lại thất bại", "奖励说明": "Mô tả phần thưởng", diff --git a/web/src/i18n/locales/zh-CN.json b/web/src/i18n/locales/zh-CN.json index 093e1557..b6f355fb 100644 --- a/web/src/i18n/locales/zh-CN.json +++ b/web/src/i18n/locales/zh-CN.json @@ -2559,6 +2559,8 @@ "重置配置": "重置配置", "重要提醒": "重要提醒", "重试": "重试", + "不重试": "不重试", + "失败后是否重试": "失败后是否重试", "重试连接": "重试连接", "钱包管理": "钱包管理", "链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1", diff --git a/web/src/i18n/locales/zh-TW.json b/web/src/i18n/locales/zh-TW.json index 0b91a509..584f3303 100644 --- a/web/src/i18n/locales/zh-TW.json +++ b/web/src/i18n/locales/zh-TW.json @@ -2569,6 +2569,8 @@ "重置配置": "重置設定", "重要提醒": "重要提醒", "重试": "重試", + "不重试": "不重試", + "失败后是否重试": "失敗後是否重試", "重试连接": "重試連接", "钱包管理": "錢包管理", "链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "連結中的{key}將自動替換為sk-xxxx,{address}將自動替換為系統設定的伺服器位址,末尾不帶/和/v1", diff --git a/web/src/pages/Setting/Operation/SettingsChannelAffinity.jsx b/web/src/pages/Setting/Operation/SettingsChannelAffinity.jsx index c179e855..5d75d68a 100644 --- a/web/src/pages/Setting/Operation/SettingsChannelAffinity.jsx +++ b/web/src/pages/Setting/Operation/SettingsChannelAffinity.jsx @@ -540,11 +540,11 @@ export default function SettingsChannelAffinity(props) { render: (v) => {Number(v || 0) || '-'}, }, { - title: t('失败后不重试'), + title: t('失败后是否重试'), dataIndex: 'skip_retry_on_failure', render: (value) => ( - - {value ? t('是') : t('否')} + + {value ? t('不重试') : t('重试')} ), }, diff --git a/web/src/pages/Setting/Performance/SettingsPerformance.jsx b/web/src/pages/Setting/Performance/SettingsPerformance.jsx index 0e30bcb6..e2767f34 100644 --- a/web/src/pages/Setting/Performance/SettingsPerformance.jsx +++ b/web/src/pages/Setting/Performance/SettingsPerformance.jsx @@ -356,7 +356,6 @@ export default function SettingsPerformance(props) { label={t('CPU 阈值 (%)')} extraText={t('CPU 使用率超过此值时拒绝请求')} min={0} - max={100} onChange={handleFieldChange( 'performance_setting.monitor_cpu_threshold', )}