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.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -48,17 +48,23 @@ func checkSystemPerformance() *types.NewAPIError {
|
|||||||
|
|
||||||
// 检查 CPU
|
// 检查 CPU
|
||||||
if config.CPUThreshold > 0 && int(status.CPUUsage) > config.CPUThreshold {
|
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 {
|
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 {
|
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
|
return nil
|
||||||
|
|||||||
@@ -2,14 +2,25 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/QuantumNous/new-api/common"
|
"github.com/QuantumNous/new-api/common"
|
||||||
"github.com/gin-gonic/gin"
|
"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) {
|
func RequestId() func(c *gin.Context) {
|
||||||
return 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)
|
c.Set(common.RequestIdKey, id)
|
||||||
ctx := context.WithValue(c.Request.Context(), common.RequestIdKey, id)
|
ctx := context.WithValue(c.Request.Context(), common.RequestIdKey, id)
|
||||||
c.Request = c.Request.WithContext(ctx)
|
c.Request = c.Request.WithContext(ctx)
|
||||||
|
|||||||
Vendored
+3
-1
@@ -1004,7 +1004,9 @@
|
|||||||
"天前": "days ago",
|
"天前": "days ago",
|
||||||
"失败": "Failed",
|
"失败": "Failed",
|
||||||
"失败原因": "Failure Reason",
|
"失败原因": "Failure Reason",
|
||||||
"失败后不重试": "No retry after failure",
|
"失败后不重试": "No Retry on Failure",
|
||||||
|
"失败后是否重试": "Retry on Failure",
|
||||||
|
"不重试": "No Retry",
|
||||||
"失败时自动禁用通道": "Automatically disable channel on failure",
|
"失败时自动禁用通道": "Automatically disable channel on failure",
|
||||||
"失败重试次数": "Failed retry times",
|
"失败重试次数": "Failed retry times",
|
||||||
"奖励说明": "Reward description",
|
"奖励说明": "Reward description",
|
||||||
|
|||||||
Vendored
+2
@@ -997,6 +997,8 @@
|
|||||||
"失败": "Échec",
|
"失败": "Échec",
|
||||||
"失败原因": "Raison de l'échec",
|
"失败原因": "Raison de l'échec",
|
||||||
"失败后不重试": "Pas de nouvelle tentative après é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",
|
"失败时自动禁用通道": "Désactiver automatiquement le canal en cas d'échec",
|
||||||
"失败重试次数": "Nombre de tentatives en cas d'échec",
|
"失败重试次数": "Nombre de tentatives en cas d'échec",
|
||||||
"奖励说明": "Description de la récompense",
|
"奖励说明": "Description de la récompense",
|
||||||
|
|||||||
Vendored
+2
@@ -988,6 +988,8 @@
|
|||||||
"失败": "失敗",
|
"失败": "失敗",
|
||||||
"失败原因": "失敗の原因",
|
"失败原因": "失敗の原因",
|
||||||
"失败后不重试": "失敗後リトライしない",
|
"失败后不重试": "失敗後リトライしない",
|
||||||
|
"失败后是否重试": "失敗後リトライ",
|
||||||
|
"不重试": "リトライしない",
|
||||||
"失败时自动禁用通道": "失敗時にチャネルを自動的に無効にする",
|
"失败时自动禁用通道": "失敗時にチャネルを自動的に無効にする",
|
||||||
"失败重试次数": "再試行回数",
|
"失败重试次数": "再試行回数",
|
||||||
"奖励说明": "特典説明",
|
"奖励说明": "特典説明",
|
||||||
|
|||||||
Vendored
+2
@@ -1003,6 +1003,8 @@
|
|||||||
"失败": "Неудача",
|
"失败": "Неудача",
|
||||||
"失败原因": "Причина ошибки",
|
"失败原因": "Причина ошибки",
|
||||||
"失败后不重试": "Не повторять после ошибки",
|
"失败后不重试": "Не повторять после ошибки",
|
||||||
|
"失败后是否重试": "Повторить при ошибке",
|
||||||
|
"不重试": "Не повторять",
|
||||||
"失败时自动禁用通道": "Автоматически отключать канал при неудаче",
|
"失败时自动禁用通道": "Автоматически отключать канал при неудаче",
|
||||||
"失败重试次数": "Количество повторных попыток при неудаче",
|
"失败重试次数": "Количество повторных попыток при неудаче",
|
||||||
"奖励说明": "Описание награды",
|
"奖励说明": "Описание награды",
|
||||||
|
|||||||
Vendored
+2
@@ -989,6 +989,8 @@
|
|||||||
"失败": "Thất bại",
|
"失败": "Thất bại",
|
||||||
"失败原因": "Nguyên nhân thất bại",
|
"失败原因": "Nguyên nhân thất bại",
|
||||||
"失败后不重试": "Không thử lại sau khi 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",
|
"失败时自动禁用通道": "Tự động vô hiệu hóa kênh khi thất bại",
|
||||||
"失败重试次数": "Số lần thử lại thất bại",
|
"失败重试次数": "Số lần thử lại thất bại",
|
||||||
"奖励说明": "Mô tả phần thưởng",
|
"奖励说明": "Mô tả phần thưởng",
|
||||||
|
|||||||
Vendored
+2
@@ -2559,6 +2559,8 @@
|
|||||||
"重置配置": "重置配置",
|
"重置配置": "重置配置",
|
||||||
"重要提醒": "重要提醒",
|
"重要提醒": "重要提醒",
|
||||||
"重试": "重试",
|
"重试": "重试",
|
||||||
|
"不重试": "不重试",
|
||||||
|
"失败后是否重试": "失败后是否重试",
|
||||||
"重试连接": "重试连接",
|
"重试连接": "重试连接",
|
||||||
"钱包管理": "钱包管理",
|
"钱包管理": "钱包管理",
|
||||||
"链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1",
|
"链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1",
|
||||||
|
|||||||
Vendored
+2
@@ -2569,6 +2569,8 @@
|
|||||||
"重置配置": "重置設定",
|
"重置配置": "重置設定",
|
||||||
"重要提醒": "重要提醒",
|
"重要提醒": "重要提醒",
|
||||||
"重试": "重試",
|
"重试": "重試",
|
||||||
|
"不重试": "不重試",
|
||||||
|
"失败后是否重试": "失敗後是否重試",
|
||||||
"重试连接": "重試連接",
|
"重试连接": "重試連接",
|
||||||
"钱包管理": "錢包管理",
|
"钱包管理": "錢包管理",
|
||||||
"链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "連結中的{key}將自動替換為sk-xxxx,{address}將自動替換為系統設定的伺服器位址,末尾不帶/和/v1",
|
"链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1": "連結中的{key}將自動替換為sk-xxxx,{address}將自動替換為系統設定的伺服器位址,末尾不帶/和/v1",
|
||||||
|
|||||||
@@ -540,11 +540,11 @@ export default function SettingsChannelAffinity(props) {
|
|||||||
render: (v) => <Text>{Number(v || 0) || '-'}</Text>,
|
render: (v) => <Text>{Number(v || 0) || '-'}</Text>,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t('失败后不重试'),
|
title: t('失败后是否重试'),
|
||||||
dataIndex: 'skip_retry_on_failure',
|
dataIndex: 'skip_retry_on_failure',
|
||||||
render: (value) => (
|
render: (value) => (
|
||||||
<Tag color={value ? 'orange' : 'grey'} style={{ marginRight: 4 }}>
|
<Tag color={value ? 'orange' : 'green'} style={{ marginRight: 4 }}>
|
||||||
{value ? t('是') : t('否')}
|
{value ? t('不重试') : t('重试')}
|
||||||
</Tag>
|
</Tag>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -356,7 +356,6 @@ export default function SettingsPerformance(props) {
|
|||||||
label={t('CPU 阈值 (%)')}
|
label={t('CPU 阈值 (%)')}
|
||||||
extraText={t('CPU 使用率超过此值时拒绝请求')}
|
extraText={t('CPU 使用率超过此值时拒绝请求')}
|
||||||
min={0}
|
min={0}
|
||||||
max={100}
|
|
||||||
onChange={handleFieldChange(
|
onChange={handleFieldChange(
|
||||||
'performance_setting.monitor_cpu_threshold',
|
'performance_setting.monitor_cpu_threshold',
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user