@@ -73,7 +73,7 @@ func (p *KeyProvider) SelectKey(groupID uint) (*models.APIKey, error) {
7373}
7474
7575// UpdateStatus 异步地提交一个 Key 状态更新任务。
76- func (p * KeyProvider ) UpdateStatus (apiKey * models.APIKey , group * models.Group , isSuccess bool ) {
76+ func (p * KeyProvider ) UpdateStatus (apiKey * models.APIKey , group * models.Group , isSuccess bool , errorMessage string ) {
7777 go func () {
7878 keyHashKey := fmt .Sprintf ("key:%d" , apiKey .ID )
7979 activeKeysListKey := fmt .Sprintf ("group:%d:active_keys" , group .ID )
@@ -83,7 +83,7 @@ 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 ); err != nil {
86+ if err := p .handleFailure (apiKey , group , keyHashKey , activeKeysListKey , errorMessage ); err != nil {
8787 logrus .WithFields (logrus.Fields {"keyID" : apiKey .ID , "error" : err }).Error ("Failed to handle key failure" )
8888 }
8989 }
@@ -117,6 +117,29 @@ func (p *KeyProvider) executeTransactionWithRetry(operation func(tx *gorm.DB) er
117117 return err
118118}
119119
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+
120143func (p * KeyProvider ) handleSuccess (keyID uint , keyHashKey , activeKeysListKey string ) error {
121144 keyDetails , err := p .store .HGetAll (keyHashKey )
122145 if err != nil {
@@ -163,7 +186,7 @@ func (p *KeyProvider) handleSuccess(keyID uint, keyHashKey, activeKeysListKey st
163186 })
164187}
165188
166- func (p * KeyProvider ) handleFailure (apiKey * models.APIKey , group * models.Group , keyHashKey , activeKeysListKey string ) error {
189+ func (p * KeyProvider ) handleFailure (apiKey * models.APIKey , group * models.Group , keyHashKey , activeKeysListKey string , errorMessage string ) error {
167190 keyDetails , err := p .store .HGetAll (keyHashKey )
168191 if err != nil {
169192 return fmt .Errorf ("failed to get key details from store: %w" , err )
@@ -175,6 +198,16 @@ func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group,
175198
176199 failureCount , _ := strconv .ParseInt (keyDetails ["failure_count" ], 10 , 64 )
177200
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+
178211 // 获取该分组的有效配置
179212 blacklistThreshold := group .EffectiveConfig .BlacklistThreshold
180213
0 commit comments