Skip to content

Commit 2d9a785

Browse files
authored
fix: sqlite测试并发优化 (#112)
1 parent 9d245b1 commit 2d9a785

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

internal/keypool/provider.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
app_errors "gpt-load/internal/errors"
88
"gpt-load/internal/models"
99
"gpt-load/internal/store"
10+
"math/rand"
1011
"strconv"
12+
"strings"
1113
"time"
1214

1315
"github.com/sirupsen/logrus"
@@ -88,6 +90,33 @@ func (p *KeyProvider) UpdateStatus(apiKey *models.APIKey, group *models.Group, i
8890
}()
8991
}
9092

93+
// executeTransactionWithRetry wraps a database transaction with a retry mechanism.
94+
func (p *KeyProvider) executeTransactionWithRetry(operation func(tx *gorm.DB) error) error {
95+
const maxRetries = 3
96+
const baseDelay = 50 * time.Millisecond
97+
const maxJitter = 150 * time.Millisecond
98+
var err error
99+
100+
for i := range maxRetries {
101+
err = p.db.Transaction(operation)
102+
if err == nil {
103+
return nil
104+
}
105+
106+
if strings.Contains(err.Error(), "database is locked") {
107+
jitter := time.Duration(rand.Intn(int(maxJitter)))
108+
totalDelay := baseDelay + jitter
109+
logrus.Debugf("Database is locked, retrying in %v... (attempt %d/%d)", totalDelay, i+1, maxRetries)
110+
time.Sleep(totalDelay)
111+
continue
112+
}
113+
114+
break
115+
}
116+
117+
return err
118+
}
119+
91120
func (p *KeyProvider) handleSuccess(keyID uint, keyHashKey, activeKeysListKey string) error {
92121
keyDetails, err := p.store.HGetAll(keyHashKey)
93122
if err != nil {
@@ -101,7 +130,7 @@ func (p *KeyProvider) handleSuccess(keyID uint, keyHashKey, activeKeysListKey st
101130
return nil
102131
}
103132

104-
return p.db.Transaction(func(tx *gorm.DB) error {
133+
return p.executeTransactionWithRetry(func(tx *gorm.DB) error {
105134
var key models.APIKey
106135
if err := tx.Set("gorm:query_option", "FOR UPDATE").First(&key, keyID).Error; err != nil {
107136
return fmt.Errorf("failed to lock key %d for update: %w", keyID, err)
@@ -149,7 +178,7 @@ func (p *KeyProvider) handleFailure(apiKey *models.APIKey, group *models.Group,
149178
// 获取该分组的有效配置
150179
blacklistThreshold := group.EffectiveConfig.BlacklistThreshold
151180

152-
return p.db.Transaction(func(tx *gorm.DB) error {
181+
return p.executeTransactionWithRetry(func(tx *gorm.DB) error {
153182
var key models.APIKey
154183
if err := tx.Set("gorm:query_option", "FOR UPDATE").First(&key, apiKey.ID).Error; err != nil {
155184
return fmt.Errorf("failed to lock key %d for update: %w", apiKey.ID, err)

internal/types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type SystemSettings struct {
3535
MaxRetries int `json:"max_retries" default:"3" name:"最大重试次数" category:"密钥配置" desc:"单个请求使用不同 Key 的最大重试次数,0为不重试。" validate:"min=0"`
3636
BlacklistThreshold int `json:"blacklist_threshold" default:"3" name:"黑名单阈值" category:"密钥配置" desc:"一个 Key 连续失败多少次后进入黑名单,0为不拉黑。" validate:"min=0"`
3737
KeyValidationIntervalMinutes int `json:"key_validation_interval_minutes" default:"60" name:"密钥验证间隔(分钟)" category:"密钥配置" desc:"后台验证密钥的默认间隔(分钟)。" validate:"min=30"`
38-
KeyValidationConcurrency int `json:"key_validation_concurrency" default:"10" name:"密钥验证并发数" category:"密钥配置" desc:"后台定时验证无效 Key 时的并发数。" validate:"min=1"`
38+
KeyValidationConcurrency int `json:"key_validation_concurrency" default:"10" name:"密钥验证并发数" category:"密钥配置" desc:"后台定时验证无效 Key 时的并发数,如果使用SQLite或者运行环境性能不佳,请尽量保证20以下,避免过高的并发导致数据不一致问题。" validate:"min=1"`
3939
KeyValidationTimeoutSeconds int `json:"key_validation_timeout_seconds" default:"20" name:"密钥验证超时(秒)" category:"密钥配置" desc:"后台定时验证单个 Key 时的 API 请求超时时间(秒)。" validate:"min=5"`
4040

4141
// For cache

0 commit comments

Comments
 (0)