Skip to content

Commit 0bcc068

Browse files
tbphpkikii16vickyyd
authored
refactor: 规范错误处理 (#202)
* solve Fix: resource has been exhausted counted as failure count * solve Fix: resource has been exhausted counted as failure count * Update provider.go * Update provider.go * Update provider.go * fix * fix2 * fix * feat: uncount error * refactor: 规范错误处理 * fix: 简化判断 * feat: 调整错误处理顺序 * docs: 增加备注 --------- Co-authored-by: kikii16 <[email protected]> Co-authored-by: kiki <[email protected]>
1 parent 060c9d9 commit 0bcc068

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package errors
2+
3+
import "strings"
4+
5+
// unCountedSubstrings contains a list of substrings that indicate an error
6+
var unCountedSubstrings = []string{
7+
"resource has been exhausted",
8+
"please reduce the length of the messages",
9+
}
10+
11+
// IsUnCounted checks if the given error message contains substrings
12+
func IsUnCounted(errorMsg string) bool {
13+
if errorMsg == "" {
14+
return false
15+
}
16+
17+
errorLower := strings.ToLower(errorMsg)
18+
19+
for _, pattern := range unCountedSubstrings {
20+
if strings.Contains(errorLower, pattern) {
21+
return true
22+
}
23+
}
24+
25+
return false
26+
}

internal/keypool/provider.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ func (p *KeyProvider) UpdateStatus(apiKey *models.APIKey, group *models.Group, i
8383
logrus.WithFields(logrus.Fields{"keyID": apiKey.ID, "error": err}).Error("Failed to handle key success")
8484
}
8585
} else {
86-
if err := p.handleFailure(apiKey, group, keyHashKey, activeKeysListKey, errorMessage); err != nil {
87-
logrus.WithFields(logrus.Fields{"keyID": apiKey.ID, "error": err}).Error("Failed to handle key failure")
86+
if app_errors.IsUnCounted(errorMessage) {
87+
logrus.WithFields(logrus.Fields{
88+
"keyID": apiKey.ID,
89+
"error": errorMessage,
90+
}).Debug("Uncounted error, skipping failure handling")
91+
} else {
92+
if err := p.handleFailure(apiKey, group, keyHashKey, activeKeysListKey); err != nil {
93+
logrus.WithFields(logrus.Fields{"keyID": apiKey.ID, "error": err}).Error("Failed to handle key failure")
94+
}
8895
}
8996
}
9097
}()
@@ -117,29 +124,6 @@ func (p *KeyProvider) executeTransactionWithRetry(operation func(tx *gorm.DB) er
117124
return err
118125
}
119126

120-
// shouldCountFailure 判断错误是否应该计入失败次数
121-
func (p *KeyProvider) shouldCountFailure(errorMsg string) bool {
122-
if errorMsg == "" {
123-
return true // 没有错误信息时默认计数
124-
}
125-
126-
// 转换为小写进行匹配
127-
errorLower := strings.ToLower(errorMsg)
128-
129-
// 不计入失败次数的错误模式
130-
excludePatterns := []string{
131-
"resource has been exhausted (e.g. check quota).", // Resource has been exhausted (e.g. check quota).
132-
}
133-
134-
for _, pattern := range excludePatterns {
135-
if strings.Contains(errorLower, pattern) {
136-
return false
137-
}
138-
}
139-
140-
return true // 其他错误计入失败次数
141-
}
142-
143127
func (p *KeyProvider) handleSuccess(keyID uint, keyHashKey, activeKeysListKey string) error {
144128
keyDetails, err := p.store.HGetAll(keyHashKey)
145129
if err != nil {
@@ -186,7 +170,7 @@ func (p *KeyProvider) handleSuccess(keyID uint, keyHashKey, activeKeysListKey st
186170
})
187171
}
188172

189-
func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group, keyHashKey, activeKeysListKey string, errorMessage string) error {
173+
func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group, keyHashKey, activeKeysListKey string) error {
190174
keyDetails, err := p.store.HGetAll(keyHashKey)
191175
if err != nil {
192176
return fmt.Errorf("failed to get key details from store: %w", err)
@@ -198,16 +182,6 @@ func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group,
198182

199183
failureCount, _ := strconv.ParseInt(keyDetails["failure_count"], 10, 64)
200184

201-
// 判断是否应该计入失败次数
202-
shouldCount := p.shouldCountFailure(errorMessage)
203-
if !shouldCount {
204-
logrus.WithFields(logrus.Fields{
205-
"keyID": apiKey.ID,
206-
"errorMessage": errorMessage,
207-
}).Debug("Error not counted towards failure threshold")
208-
return nil // 不计入失败次数,直接返回
209-
}
210-
211185
// 获取该分组的有效配置
212186
blacklistThreshold := group.EffectiveConfig.BlacklistThreshold
213187

internal/proxy/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
202202
if err != nil {
203203
statusCode = 500
204204
errorMessage = err.Error()
205-
parsedError = errorMessage // 网络错误直接使用原始错误信息
205+
parsedError = errorMessage
206206
logrus.Debugf("Request failed (attempt %d/%d) for key %s: %v", retryCount+1, cfg.MaxRetries, utils.MaskAPIKey(apiKey.KeyValue), err)
207207
} else {
208208
// HTTP-level error (status >= 400)

0 commit comments

Comments
 (0)