Skip to content

Commit 57c14ca

Browse files
committed
feat: add lazy account mode
1 parent 0627e0b commit 57c14ca

10 files changed

Lines changed: 254 additions & 29 deletions

File tree

admin/bootstrap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func defaultBootstrapSettings() *database.SystemSettings {
220220
BackgroundRefreshIntervalMinutes: 2,
221221
UsageProbeMaxAgeMinutes: 10,
222222
RecoveryProbeIntervalMinutes: 30,
223+
LazyMode: false,
223224
PgMaxConns: 50,
224225
RedisPoolSize: 30,
225226
PromptFilterMode: "monitor",

admin/handler.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ func (h *Handler) ListAccounts(c *gin.Context) {
517517
Status: row.Status,
518518
ErrorMessage: row.ErrorMessage,
519519
ATOnly: !isOpenAIResponsesAccount && row.GetCredential("refresh_token") == "" && row.GetCredential("access_token") != "",
520-
CreditEnabled: row.CreditEnabled,
521-
CreditSkipUsageWindow: row.CreditSkipUsageWindow,
520+
CreditEnabled: row.CreditEnabled,
521+
CreditSkipUsageWindow: row.CreditSkipUsageWindow,
522522
AccountType: row.Type,
523523
OpenAIResponsesAPI: isOpenAIResponsesAccount,
524524
BaseURL: baseURL,
@@ -1191,16 +1191,18 @@ func (h *Handler) AddAccount(c *gin.Context) {
11911191
}
11921192
h.store.AddAccount(newAcc)
11931193

1194-
// 异步刷新 AT
1195-
go func(accountID int64) {
1196-
refreshCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
1197-
defer cancel()
1198-
if err := h.store.RefreshSingle(refreshCtx, accountID); err != nil {
1199-
log.Printf("新账号 %d 刷新失败: %v", accountID, err)
1200-
} else {
1201-
log.Printf("新账号 %d 刷新成功,已加入号池", accountID)
1202-
}
1203-
}(id)
1194+
if !h.store.GetLazyMode() {
1195+
// 异步刷新 AT
1196+
go func(accountID int64) {
1197+
refreshCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
1198+
defer cancel()
1199+
if err := h.store.RefreshSingle(refreshCtx, accountID); err != nil {
1200+
log.Printf("新账号 %d 刷新失败: %v", accountID, err)
1201+
} else {
1202+
log.Printf("新账号 %d 刷新成功,已加入号池", accountID)
1203+
}
1204+
}(id)
1205+
}
12041206
}
12051207

12061208
// 记录安全审计日志
@@ -2300,7 +2302,7 @@ func (h *Handler) importAccountsCommon(c *gin.Context, tokens []importToken, pro
23002302
newAcc := accountFromCredentialSeed(id, proxyURL, seed)
23012303
h.store.AddAccount(newAcc)
23022304

2303-
if tok.accessToken == "" {
2305+
if tok.accessToken == "" && !h.store.GetLazyMode() {
23042306
// 后台异步刷新,不阻塞导入流程
23052307
go func(accountID int64) {
23062308
refreshCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -3561,6 +3563,7 @@ type settingsResponse struct {
35613563
BackgroundRefreshIntervalMinutes int `json:"background_refresh_interval_minutes"`
35623564
UsageProbeMaxAgeMinutes int `json:"usage_probe_max_age_minutes"`
35633565
RecoveryProbeIntervalMinutes int `json:"recovery_probe_interval_minutes"`
3566+
LazyMode bool `json:"lazy_mode"`
35643567
ProxyURL string `json:"proxy_url"`
35653568
PgMaxConns int `json:"pg_max_conns"`
35663569
RedisPoolSize int `json:"redis_pool_size"`
@@ -3621,6 +3624,7 @@ type updateSettingsReq struct {
36213624
BackgroundRefreshIntervalMinutes *int `json:"background_refresh_interval_minutes"`
36223625
UsageProbeMaxAgeMinutes *int `json:"usage_probe_max_age_minutes"`
36233626
RecoveryProbeIntervalMinutes *int `json:"recovery_probe_interval_minutes"`
3627+
LazyMode *bool `json:"lazy_mode"`
36243628
ProxyURL *string `json:"proxy_url"`
36253629
PgMaxConns *int `json:"pg_max_conns"`
36263630
RedisPoolSize *int `json:"redis_pool_size"`
@@ -3761,6 +3765,7 @@ func (h *Handler) GetSettings(c *gin.Context) {
37613765
BackgroundRefreshIntervalMinutes: h.store.GetBackgroundRefreshIntervalMinutes(),
37623766
UsageProbeMaxAgeMinutes: h.store.GetUsageProbeMaxAgeMinutes(),
37633767
RecoveryProbeIntervalMinutes: h.store.GetRecoveryProbeIntervalMinutes(),
3768+
LazyMode: h.store.GetLazyMode(),
37643769
ProxyURL: h.store.GetProxyURL(),
37653770
PgMaxConns: h.pgMaxConns,
37663771
RedisPoolSize: h.redisPoolSize,
@@ -3773,7 +3778,7 @@ func (h *Handler) GetSettings(c *gin.Context) {
37733778
AutoCleanExpired: h.store.GetAutoCleanExpired(),
37743779
ProxyPoolEnabled: h.store.GetProxyPoolEnabled(),
37753780
FastSchedulerEnabled: h.store.FastSchedulerEnabled(),
3776-
SchedulerMode: h.store.GetSchedulerMode(),
3781+
SchedulerMode: h.store.GetSchedulerMode(),
37773782
MaxRetries: h.store.GetMaxRetries(),
37783783
MaxRateLimitRetries: h.store.GetMaxRateLimitRetries(),
37793784
AllowRemoteMigration: h.store.GetAllowRemoteMigration() && adminAuthSource != "disabled",
@@ -3929,6 +3934,11 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
39293934
log.Printf("设置已更新: recovery_probe_interval_minutes = %d", v)
39303935
}
39313936

3937+
if req.LazyMode != nil {
3938+
h.store.SetLazyMode(*req.LazyMode)
3939+
log.Printf("设置已更新: lazy_mode = %t", *req.LazyMode)
3940+
}
3941+
39323942
if req.ProxyURL != nil {
39333943
h.store.SetProxyURL(*req.ProxyURL)
39343944
log.Printf("设置已更新: proxy_url = %s", *req.ProxyURL)
@@ -4241,6 +4251,7 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
42414251
BackgroundRefreshIntervalMinutes: h.store.GetBackgroundRefreshIntervalMinutes(),
42424252
UsageProbeMaxAgeMinutes: h.store.GetUsageProbeMaxAgeMinutes(),
42434253
RecoveryProbeIntervalMinutes: h.store.GetRecoveryProbeIntervalMinutes(),
4254+
LazyMode: h.store.GetLazyMode(),
42444255
ProxyURL: h.store.GetProxyURL(),
42454256
PgMaxConns: h.pgMaxConns,
42464257
RedisPoolSize: h.redisPoolSize,
@@ -4252,7 +4263,7 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
42524263
AutoCleanExpired: h.store.GetAutoCleanExpired(),
42534264
ProxyPoolEnabled: h.store.GetProxyPoolEnabled(),
42544265
FastSchedulerEnabled: h.store.FastSchedulerEnabled(),
4255-
SchedulerMode: h.store.GetSchedulerMode(),
4266+
SchedulerMode: h.store.GetSchedulerMode(),
42564267
MaxRetries: h.store.GetMaxRetries(),
42574268
MaxRateLimitRetries: h.store.GetMaxRateLimitRetries(),
42584269
AllowRemoteMigration: h.store.GetAllowRemoteMigration() && hasAdminSecret,
@@ -4304,6 +4315,7 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
43044315
BackgroundRefreshIntervalMinutes: h.store.GetBackgroundRefreshIntervalMinutes(),
43054316
UsageProbeMaxAgeMinutes: h.store.GetUsageProbeMaxAgeMinutes(),
43064317
RecoveryProbeIntervalMinutes: h.store.GetRecoveryProbeIntervalMinutes(),
4318+
LazyMode: h.store.GetLazyMode(),
43074319
ProxyURL: h.store.GetProxyURL(),
43084320
PgMaxConns: h.pgMaxConns,
43094321
RedisPoolSize: h.redisPoolSize,
@@ -4316,7 +4328,7 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
43164328
AutoCleanExpired: h.store.GetAutoCleanExpired(),
43174329
ProxyPoolEnabled: h.store.GetProxyPoolEnabled(),
43184330
FastSchedulerEnabled: h.store.FastSchedulerEnabled(),
4319-
SchedulerMode: h.store.GetSchedulerMode(),
4331+
SchedulerMode: h.store.GetSchedulerMode(),
43204332
MaxRetries: h.store.GetMaxRetries(),
43214333
MaxRateLimitRetries: h.store.GetMaxRateLimitRetries(),
43224334
AllowRemoteMigration: h.store.GetAllowRemoteMigration() && adminAuthSource != "disabled",

0 commit comments

Comments
 (0)