diff --git a/README.md b/README.md index 7b1ed6ee..ee5b7a8e 100644 --- a/README.md +++ b/README.md @@ -801,7 +801,7 @@ API 试用页的 HTML 预览运行在 `PREVIEW_LISTEN_ADDR` 提供的独立来 | `auth_required` | 编辑、改为就绪、停用、重新登录、立即刷新、删除 | | `restricted` | 编辑、改为就绪、停用、立即刷新、删除 | -- 改为就绪会启用已停用的账户,并清除服务受限、冷却与需要登录的标记;账户仍被 Google 拒绝时,下一次请求把它标回原状态并换用其他账户 +- 改为就绪会启用已停用的账户,并清除服务受限、账户整体的冷却与需要登录的标记,各模型的冷却保留到恢复时间;账户仍被 Google 拒绝时,下一次请求把它标回原状态并换用其他账户 - 刷新、停用、启用与保存在进行时,账户行显示当前步骤与已用时间(等待几个请求结束、读取权益与模型目录、核对验证状态),日志页有同样的步骤与结果 权益与模型目录的读取: diff --git a/README_en.md b/README_en.md index 926cf5bc..2756eec8 100644 --- a/README_en.md +++ b/README_en.md @@ -800,7 +800,7 @@ Account actions depend on state. Click the state in an account row to set the ac | `auth_required` | Edit, set to Ready, disable, sign in again, refresh now, delete | | `restricted` | Edit, set to Ready, disable, refresh now, delete | -- Setting an account to Ready enables a disabled account and clears the service restriction, cooldown, and sign-in required marks; if Google still rejects the account, the next request marks it again and switches to another account +- Setting an account to Ready enables a disabled account and clears the service restriction, the account-wide cooldown, and the sign-in required mark, while per-model cooldowns stay until they recover; if Google still rejects the account, the next request marks it again and switches to another account - While a refresh, disable, enable, or save is in progress, the account row shows the current step and elapsed time (how many requests it is waiting for, reading the plan and models, checking verification), and Logs shows the same steps and the result Reading the benefit tier and model catalog: diff --git a/docs/development.md b/docs/development.md index be49a29e..e2d2966b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -529,7 +529,7 @@ POST /api/control/start | `benefit_tier` | 账户权益:`0` Free、`1` Pro、`2` Ultra、`3` Plus | | `catalog_fingerprint` | 目录指纹,与 `catalog.json` 一致时采用目录缓存 | | `model_access` | `{state,checked_at,reason?}`,成功状态为 `verified` | -| `cooldowns` | `{until,reason?,level?,fallback?,rejected_size?,rejected_until?}` | +| `cooldowns` | `{until,reason?,level?,fallback?,rejected_size?,rejected_until?,rejected_level?}` | | `resources` | `{kind?,name?,mime?,size?,purpose?,created_at,video?,alias?,sha256?,video_duration?,used_at?,display_name?,expires_at?}` | | `proxy` | 从 `PROXY` 代理池分配给账户的代理 | | `restricted` | Google 拒绝账户使用 AI Studio 的原因与跳转页 | @@ -646,8 +646,8 @@ Worker 容量由热池目标、活动上限和单账户并发共同约束。 全部候选账户都在冷却,且本请求累计排队已到 1 分钟、最早恢复晚于剩余时长或最早恢复的是权限拒绝冷却时,套餐限额、每日限额与权限拒绝冷却中的账户仍接收请求。上游按每个请求判定是否放行:额度用尽后较短的请求仍可能完成,权限拒绝也是间歇出现。 - 同一请求依次发往这些账户、每个账户一次,上游放行即完成 -- 被拒的账户按上游原因更新冷却,全部拒绝时按最后一次的原因返回 429 或 503 -- 每日限额冷却中的账户被拒后,记下被拒请求的输入字节数(系统指令、各轮文本、函数参数与结果、内联附件);1 分钟内输入字节数不小于它的请求不再发往该账户,记下的字节数越大的账户越先使用 +- 被拒的账户按上游原因更新冷却,套餐限额的恢复时间只精确到分钟,再次被拒算出的恢复时刻与已有冷却相差不到 1 分钟时沿用已有的恢复时刻;全部拒绝时按最后一次的原因返回 429 或 503 +- 套餐限额与每日限额冷却中的账户被拒后,记下被拒请求的输入字节数(系统指令、各轮文本、函数参数与结果、内联附件);记录有效期为 5 分钟,失效后同一账户同一模型再次被拒时依次为 15、30、60 分钟,成功一次后从 5 分钟重新开始,有效期内再次被拒只把记下的字节数改为较小的一个;有效期内输入字节数不小于它的请求跳过该账户,更小的请求照常发送,记下的字节数越大的账户越先使用;被跳过的账户按记录失效与冷却恢复中较早的时刻计入重试时间 - 冷却中的账户在这类发送中收到恢复更早的其他额度错误(如不带细节的 429)时,该错误另记为回退冷却,按连续次数 1、5、15、30、60 分钟递增,原冷却不变;回退冷却未结束的账户不再接收这类发送 - 没有可发的账户时直接返回 429 与最早恢复时间 diff --git a/docs/protocol.md b/docs/protocol.md index d0f13da8..07b60628 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -1919,7 +1919,7 @@ Chrome 导入列表按 `Preferences.account_info` 中的 Gaia ID 与邮箱逐个 新增、导入与登录在保存账户后立即返回,该账户的模型目录在后台同步,期间 `AdminAccount.transition` 为 `syncing`,同步结束后推送最新账户与模型事件;同步随生成服务的生命周期结束。验证成功后同样刷新该账户模型目录。 -`PUT /api/accounts/{id}/state` 的 body 为 `{"state":"ready"|"disabled"}`:`disabled` 与停用账户相同;`ready` 先启用停用的账户,再清除服务受限、全部冷却与需要重新登录的标记(删除 `runtime-state.json` 中的受限与冷却记录),账户回到 `ready`,之后由下一次请求按上游结果重新判定,仍被上游拒绝时照常标回并换用其他账户;之后推送 `cooldowns`、`models` 与 `accounts` 事件。其他取值返回 400 `invalid_account`,账户不存在时返回 404 `account_not_found`。 +`PUT /api/accounts/{id}/state` 的 body 为 `{"state":"ready"|"disabled"}`:`disabled` 与停用账户相同;`ready` 先启用停用的账户,再清除服务受限、全局冷却与需要重新登录的标记(删除 `runtime-state.json` 中的受限与全局冷却记录,各模型的冷却保留,由 `DELETE /api/accounts/{id}/cooldowns/{scope}` 逐个清除),账户回到 `ready`,之后由下一次请求按上游结果重新判定,仍被上游拒绝时照常标回并换用其他账户;之后推送 `cooldowns`、`models` 与 `accounts` 事件。其他取值返回 400 `invalid_account`,账户不存在时返回 404 `account_not_found`。 `PUT /api/accounts/{id}` 只切换 `enabled` 时立即写回 `account.json` 并返回,账户状态同时更新,后台收尾的操作写在 `AdminAccount.transition`,结束后字段省略;再次切换同一账户会取消进行中的收尾。 @@ -3927,7 +3927,7 @@ Gemini 列中注明状态码的行以该状态码返回;Interactions 路由与 | 视频仍在生成 | 409 | `video_not_ready` | `api_error` | `INTERNAL` | | 文件超过 512 MiB | 413 | `file_too_large` | `request_too_large` | `INTERNAL` | | 上游配额或限流(上游给出重试时长时带 `Retry-After`,Gemini 错误体带 `RetryInfo`) | 429 | `rate_limit_exceeded` | `rate_limit_error` | `RESOURCE_EXHAUSTED` | -| 候选账户均在冷却且在本请求累计排队 1 分钟内不恢复,或已试账户都因额度失败而其余账户在冷却(套餐限额与每日限额冷却中的账户逐个发送一次,其中收到其他额度失败的账户在该失败的冷却结束前不再这样发送,全部拒绝时返回最后一次拒绝;带 `Retry-After`,Gemini 错误体带 `RetryInfo`) | 429 | `rate_limit_exceeded` | `rate_limit_error` | `RESOURCE_EXHAUSTED` | +| 候选账户均在冷却且在本请求累计排队 1 分钟内不恢复,或已试账户都因额度失败而其余账户在冷却(套餐限额与每日限额冷却中的账户逐个发送一次,其中 5 到 60 分钟内拒绝过同样大或更小请求的账户、收到其他额度失败且该失败的冷却没有结束的账户跳过,全部拒绝时返回最后一次拒绝,全部跳过时立即返回;带 `Retry-After`,Gemini 错误体带 `RetryInfo`) | 429 | `rate_limit_exceeded` | `rate_limit_error` | `RESOURCE_EXHAUSTED` | | 上游过载 | 529 | `upstream_error` | `overloaded_error` | `INTERNAL` | | 当前客户端请求被管理端取消 | 503 | `request_canceled` | `api_error` | `UNAVAILABLE` | | 生成服务已停止 | 503 | `service_stopped` | `api_error` | `UNAVAILABLE` | diff --git a/docs/reverse-engineering.md b/docs/reverse-engineering.md index b6a5083e..177ff198 100644 --- a/docs/reverse-engineering.md +++ b/docs/reverse-engineering.md @@ -428,7 +428,7 @@ Bidi setup 成功使用 lease(本次会话持有的账户租约)的 `checked | `benefit_tier` | 账户权益 | | `catalog_fingerprint` | 目录指纹 | | `model_access` | `{state,checked_at,reason?}` | -| `cooldowns` | `{until,reason?,level?,fallback?,rejected_size?,rejected_until?}` | +| `cooldowns` | `{until,reason?,level?,fallback?,rejected_size?,rejected_until?,rejected_level?}` | | `resources` | `{kind?,name?,mime?,size?,purpose?,created_at,video?,alias?,sha256?,video_duration?,used_at?,display_name?,expires_at?}` | | `proxy` | 从 `PROXY` 代理池分配给账户的代理 | | `restricted` | Google 拒绝账户使用 AI Studio 的原因与跳转页 | diff --git a/internal/aistudio/accounts.go b/internal/aistudio/accounts.go index 6b3ca6ce..10d7924f 100644 --- a/internal/aistudio/accounts.go +++ b/internal/aistudio/accounts.go @@ -439,7 +439,7 @@ type AccountSelection struct { PlaygroundOnly bool Channel Channel PreferredChannel Channel - // IgnoreQuotaCooldown 为 true 时套餐限额、每日限额与权限拒绝冷却中的账户照常参与选号,每日限额一分钟内拒绝过不大于 RequestSize 的请求的账户除外,余量多的账户优先 + // IgnoreQuotaCooldown 为 true 时套餐限额、每日限额与权限拒绝冷却中的账户照常参与选号,拒绝记录有效期内拒绝过不大于 RequestSize 的请求的账户除外,余量多的账户优先 IgnoreQuotaCooldown bool // RequestSize 是请求输入正文的字节数,0 表示未知 RequestSize int64 @@ -2506,7 +2506,7 @@ func (p *AccountPool) MarkCooldownIfGeneration( return err } -// MarkQuotaCooldownIfGeneration 保存当前目录版本下检查时间为 checkedAt 的额度冷却,递增冷却按同范围上一次冷却计算,同范围已有更晚恢复的冷却时保留它,套餐限额或每日限额冷却期间的其他额度失败只冷却回退选号,换成恢复更晚的同类冷却时回退选号的冷却保留;返回该冷却的恢复时刻与冷却是否因此改变,结果早于已记录的检查时间时恢复时刻为零值 +// MarkQuotaCooldownIfGeneration 保存当前目录版本下检查时间为 checkedAt 的额度冷却,递增冷却按同范围上一次冷却计算,同范围已有恢复更晚的冷却或恢复时刻相差不到一分钟的套餐限额冷却时保留它,套餐限额或每日限额冷却期间的其他额度失败只冷却回退选号,换成恢复更晚的同类冷却时回退选号的冷却保留;返回该冷却的恢复时刻与冷却是否因此改变,结果早于已记录的检查时间时恢复时刻为零值 func (p *AccountPool) MarkQuotaCooldownIfGeneration( accountID string, modelAccessScope string, @@ -2551,14 +2551,11 @@ func (p *AccountPool) MarkQuotaCooldownIfGeneration( case cooldown.Escalate: next = planLimitCooldown(base, baseExists, now, cooldown.Reason) } - if baseExists && base.Active(now) && !next.Until.After(base.Until) { + if baseExists && base.Active(now) && (!next.Until.After(base.Until) || samePlanWindow(base, next)) { next = base } - if strings.HasPrefix(cooldown.Reason, DailyLimitKind) && cooldown.RequestSize > 0 { - next.RejectedSize, next.RejectedUntil = cooldown.RequestSize, now.Add(rejectedSizeWindow) - if exists && now.Before(previous.RejectedUntil) { - next.RejectedSize = min(next.RejectedSize, previous.RejectedSize) - } + if RequestQuotaCooldown(cooldown.Reason) { + next = next.withRejection(previous, cooldown.RequestSize, now) } until = next.Until if layered && next.Until.Before(previous.Until) { @@ -2833,12 +2830,13 @@ func (p *AccountPool) clearRestriction(accountID string) error { return err } -// ClearAccountState 清除账户的服务受限、全部冷却与需要重新登录的标记并删除 runtime-state.json 中的受限与冷却记录,启用的账户回到就绪 +// ClearAccountState 清除账户的服务受限、全局冷却与需要重新登录的标记并删除 runtime-state.json 中的受限与全局冷却记录,各模型的冷却保留,启用的账户回到就绪 func (p *AccountPool) ClearAccountState(accountID string) error { _, err := p.updateRuntime(accountID, func(_ *Account, runtimeState *accountRuntimeState) (bool, func(*Account), error) { - changed := runtimeState.Restricted != "" || len(runtimeState.Cooldowns) > 0 + _, cooling := runtimeState.Cooldowns[globalCooldownKey] + changed := runtimeState.Restricted != "" || cooling runtimeState.Restricted = "" - clear(runtimeState.Cooldowns) + delete(runtimeState.Cooldowns, globalCooldownKey) return changed, func(account *Account) { if account.Config.Enabled { account.State, account.stateMessage = AccountReady, "" @@ -4126,7 +4124,7 @@ func accountCatalogFingerprint(tier BenefitTier, models []Model) (string, error) return fmt.Sprintf("%x", sha256.Sum256(data)), nil } -// quotaHeadroomLocked 返回候选在回退选号中的额度余量:每日限额冷却中为一分钟内上游拒绝过的最小请求字节数,没有拒绝记录时为最大值 +// quotaHeadroomLocked 返回候选在回退选号中的额度余量:套餐限额或每日限额冷却中为有效的拒绝记录记下的最小请求字节数,没有时为最大值 func (p *AccountPool) quotaHeadroomLocked(candidate channelCandidate, selection AccountSelection, now time.Time) int64 { account := p.accounts[candidate.index] cooldown, exists := account.runtime.Cooldowns[ChannelCooldownScope(candidate.channel, selectionAccessScope(selection))] @@ -4136,7 +4134,7 @@ func (p *AccountPool) quotaHeadroomLocked(candidate channelCandidate, selection return cooldown.RejectedSize } -// selectionCooldown 返回账户在作用域上对选择生效的冷却,IgnoreQuotaCooldown 的选择不受套餐限额、每日限额与权限拒绝冷却限制,只受其中记录的非权限拒绝回退冷却与每日限额一分钟内拒绝过不大于 RequestSize 的请求的拒绝记录限制,生效到两者中较晚结束的一个 +// selectionCooldown 返回账户在作用域上对选择生效的冷却,IgnoreQuotaCooldown 的选择不受套餐限额、每日限额与权限拒绝冷却限制,只受其中记录的非权限拒绝回退冷却与拒绝过不大于 RequestSize 的请求的有效拒绝记录限制,拒绝记录生效到它失效与冷却恢复中较早的一个,两种限制都有时生效到较晚结束的一个 func selectionCooldown(account *Account, scope string, selection AccountSelection, now time.Time) (CooldownState, bool) { var selected CooldownState for _, key := range []string{globalCooldownKey, scope} { @@ -4151,7 +4149,9 @@ func selectionCooldown(account *Account, scope string, selection AccountSelectio blocking := CooldownState{} if cooldown.rejects(selection.RequestSize, now) { blocking = cooldown - blocking.Until = cooldown.RejectedUntil + if cooldown.RejectedUntil.Before(cooldown.Until) { + blocking.Until = cooldown.RejectedUntil + } } if fallback := cooldown.Fallback; fallback != nil && fallback.Active(now) && !strings.HasPrefix(fallback.Reason, AccountRejectionKind) && fallback.Until.After(blocking.Until) { blocking = *fallback diff --git a/internal/aistudio/quota.go b/internal/aistudio/quota.go index 6ff60b61..20b962f7 100644 --- a/internal/aistudio/quota.go +++ b/internal/aistudio/quota.go @@ -32,12 +32,12 @@ func quotaCooldownReason(reason string) bool { return slices.ContainsFunc(quotaCooldownKinds, func(kind string) bool { return strings.HasPrefix(reason, kind+": ") }) } -// rejectedSizeWindow 是每日限额拒绝过的请求大小用于跳过不小于它的回退请求的时长 -const rejectedSizeWindow = time.Minute - // planLimitBackoff 为没有恢复时间的套餐限额与短期限额按连续次数递增的冷却时长 var planLimitBackoff = []time.Duration{time.Minute, 5 * time.Minute, 15 * time.Minute, 30 * time.Minute, time.Hour} +// rejectedSizeBackoff 为套餐限额与每日限额的拒绝记录失效后再次被拒时依次进入的有效期 +var rejectedSizeBackoff = []time.Duration{5 * time.Minute, 15 * time.Minute, 30 * time.Minute, time.Hour} + // quotaLimitPattern 匹配额度错误中逐项列出的额度上限 var quotaLimitPattern = regexp.MustCompile(`limit: (\d+)`) @@ -51,10 +51,12 @@ type CooldownState struct { Level int `json:"level,omitempty"` // Fallback 是套餐限额或每日限额冷却期间,忽略这两类冷却的回退选号另受的较早结束的冷却 Fallback *CooldownState `json:"fallback,omitempty"` - // RejectedSize 是每日限额冷却期间上游在 RejectedUntil 之前的一分钟内以额度拒绝过的最小请求输入字节数 + // RejectedSize 是套餐限额或每日限额冷却期间上游在 RejectedUntil 之前以额度拒绝过的最小请求输入字节数 RejectedSize int64 `json:"rejected_size,omitempty"` // RejectedUntil 是 RejectedSize 失效的时刻 RejectedUntil time.Time `json:"rejected_until,omitzero"` + // RejectedLevel 是拒绝记录有效期在 rejectedSizeBackoff 中的档位,取值范围 1..len(rejectedSizeBackoff) + RejectedLevel int `json:"rejected_level,omitempty"` } // Active 判断冷却状态当前是否生效 @@ -62,7 +64,7 @@ func (c CooldownState) Active(now time.Time) bool { return !c.Until.IsZero() && now.Before(c.Until) } -// rejects 判断上游一分钟内以每日限额拒绝过不大于 size 的请求 +// rejects 判断上游在拒绝记录有效期内以套餐限额或每日限额拒绝过不大于 size 的请求 func (c CooldownState) rejects(size int64, now time.Time) bool { return now.Before(c.RejectedUntil) && size >= c.RejectedSize } @@ -183,6 +185,25 @@ func planLimitCooldown(previous CooldownState, exists bool, now time.Time, reaso return CooldownState{Until: now.Add(planLimitBackoff[level-1]).UTC(), Reason: reason, Level: level} } +// samePlanWindow 判断两个套餐限额冷却的恢复时刻相差不到一分钟,套餐限额的恢复时间只精确到分钟 +func samePlanWindow(previous, next CooldownState) bool { + return strings.HasPrefix(previous.Reason, PlanLimitKind) && strings.HasPrefix(next.Reason, PlanLimitKind) && next.Until.Sub(previous.Until).Abs() < time.Minute +} + +// withRejection 返回沿用 previous 的拒绝记录的冷却,size 大于 0 时再记下上游以套餐限额或每日限额拒绝了 size 字节的请求:记录仍有效时取较小的字节数,已失效时进入下一档有效期 +func (c CooldownState) withRejection(previous CooldownState, size int64, now time.Time) CooldownState { + c.RejectedSize, c.RejectedUntil, c.RejectedLevel = previous.RejectedSize, previous.RejectedUntil, previous.RejectedLevel + switch { + case size <= 0: + case now.Before(previous.RejectedUntil): + c.RejectedSize = min(size, previous.RejectedSize) + default: + c.RejectedLevel = min(previous.RejectedLevel+1, len(rejectedSizeBackoff)) + c.RejectedSize, c.RejectedUntil = size, now.Add(rejectedSizeBackoff[c.RejectedLevel-1]).UTC() + } + return c +} + func minuteQuotaEvidence(value string) bool { return strings.Contains(value, "/min/") || strings.Contains(value, "perminute") || strings.Contains(value, "per_min") || strings.Contains(value, "per minute") diff --git a/internal/app/admin.go b/internal/app/admin.go index 2b789c73..0f93ad1e 100644 --- a/internal/app/admin.go +++ b/internal/app/admin.go @@ -747,7 +747,7 @@ func (admin *runtimeAdmin) UpdateAccount(ctx context.Context, accountID string, return target.syncedAccount(enabled) } -// SetAccountState 在提交时的生成服务把账户改为 state:disabled 停用账户;ready 先启用停用的账户,再清除服务受限、全部冷却与需要重新登录的标记,之后由下一次请求按上游结果重新判定 +// SetAccountState 在提交时的生成服务把账户改为 state:disabled 停用账户;ready 先启用停用的账户,再清除服务受限、全局冷却与需要重新登录的标记(各模型的冷却保留),之后由下一次请求按上游结果重新判定 func (admin *runtimeAdmin) SetAccountState(_ context.Context, accountID string, state string) (api.AdminAccount, error) { target, done := admin.accountCommit() defer done() @@ -760,7 +760,7 @@ func (admin *runtimeAdmin) SetAccountState(_ context.Context, accountID string, return api.AdminAccount{}, invalidAccount(fmt.Errorf("账户状态只能改为 ready 或 disabled: %q", state)) } -// restoreAccount 启用停用的账户并清除它的服务受限、全部冷却与需要重新登录的标记,已启用而没有模型目录的账户在后台同步目录;写一条日志并推送冷却、目录与账户状态 +// restoreAccount 启用停用的账户并清除它的服务受限、全局冷却与需要重新登录的标记,已启用而没有模型目录的账户在后台同步目录;写一条日志并推送冷却、目录与账户状态 func (admin *runtimeAdmin) restoreAccount(accountID string) (api.AdminAccount, error) { current, err := admin.account(accountID) if err != nil {