feat: 定时任务重构调整key验证策略

This commit is contained in:
tbphp
2025-07-13 19:27:04 +08:00
parent 92697251ee
commit 37bdcf2e72
13 changed files with 51 additions and 175 deletions

View File

@@ -140,12 +140,12 @@ func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group,
return fmt.Errorf("failed to get key details from store: %w", err)
}
failureCount, _ := strconv.ParseInt(keyDetails["failure_count"], 10, 64)
if keyDetails["status"] == models.KeyStatusInvalid {
return nil
}
failureCount, _ := strconv.ParseInt(keyDetails["failure_count"], 10, 64)
// 获取该分组的有效配置
blacklistThreshold := group.EffectiveConfig.BlacklistThreshold

View File

@@ -6,6 +6,7 @@ import (
"gpt-load/internal/channel"
"gpt-load/internal/config"
"gpt-load/internal/models"
"time"
"github.com/sirupsen/logrus"
"go.uber.org/dig"
@@ -46,10 +47,9 @@ func NewKeyValidator(params KeyValidatorParams) *KeyValidator {
}
// ValidateSingleKey performs a validation check on a single API key.
func (s *KeyValidator) ValidateSingleKey(ctx context.Context, key *models.APIKey, group *models.Group) (bool, error) {
if ctx.Err() != nil {
return false, fmt.Errorf("context cancelled or timed out: %w", ctx.Err())
}
func (s *KeyValidator) ValidateSingleKey(key *models.APIKey, group *models.Group) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ch, err := s.channelFactory.GetChannel(group)
if err != nil {
@@ -79,7 +79,7 @@ func (s *KeyValidator) ValidateSingleKey(ctx context.Context, key *models.APIKey
}
// TestMultipleKeys performs a synchronous validation for a list of key values within a specific group.
func (s *KeyValidator) TestMultipleKeys(ctx context.Context, group *models.Group, keyValues []string) ([]KeyTestResult, error) {
func (s *KeyValidator) TestMultipleKeys(group *models.Group, keyValues []string) ([]KeyTestResult, error) {
results := make([]KeyTestResult, len(keyValues))
// Find which of the provided keys actually exist in the database for this group
@@ -103,7 +103,8 @@ func (s *KeyValidator) TestMultipleKeys(ctx context.Context, group *models.Group
continue
}
isValid, validationErr := s.ValidateSingleKey(ctx, &apiKey, group)
isValid, validationErr := s.ValidateSingleKey(&apiKey, group)
results[i] = KeyTestResult{
KeyValue: kv,
IsValid: isValid,