feat: persist account cooldowns

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
zyycn
2026-06-13 11:47:12 +08:00
co-authored by Codex
parent 161c8e60eb
commit 47c338e8ab
8 changed files with 153 additions and 11 deletions
+40 -4
View File
@@ -322,7 +322,7 @@ impl AccountRepository {
pub async fn list_pool_accounts(&self) -> AccountRepositoryResult<Vec<Account>> {
let rows = sqlx::query(
"select accounts.id, email, accounts.account_id, user_id, label, plan_type, access_token_cipher, refresh_token_cipher, access_token_expires_at, status, added_at, updated_at, account_usage.last_used_at as usage_last_used_at from accounts left join account_usage on account_usage.account_id = accounts.id order by added_at desc, accounts.id desc",
"select accounts.id, email, accounts.account_id, user_id, label, plan_type, access_token_cipher, refresh_token_cipher, access_token_expires_at, status, added_at, updated_at, quota_limit_reached, quota_cooldown_until, cloudflare_cooldown_until, account_usage.last_used_at as usage_last_used_at from accounts left join account_usage on account_usage.account_id = accounts.id order by added_at desc, accounts.id desc",
)
.fetch_all(&self.pool)
.await?;
@@ -345,9 +345,13 @@ impl AccountRepository {
refresh_token,
access_token_expires_at: stored.access_token_expires_at,
status: stored.status,
quota_limit_reached: false,
quota_cooldown_until: None,
cloudflare_cooldown_until: None,
quota_limit_reached: row.get::<i64, _>("quota_limit_reached") != 0,
quota_cooldown_until: parse_optional_rfc3339(
row.get::<Option<String>, _>("quota_cooldown_until"),
)?,
cloudflare_cooldown_until: parse_optional_rfc3339(
row.get::<Option<String>, _>("cloudflare_cooldown_until"),
)?,
added_at: stored.added_at.to_rfc3339(),
last_used_at: row.get("usage_last_used_at"),
});
@@ -545,6 +549,38 @@ impl AccountRepository {
Ok(result.rows_affected() > 0)
}
pub async fn set_quota_cooldown_until(
&self,
id: &str,
cooldown_until: DateTime<Utc>,
) -> AccountRepositoryResult<bool> {
let result = sqlx::query(
"update accounts set quota_limit_reached = 1, quota_cooldown_until = ?, updated_at = ? where id = ?",
)
.bind(cooldown_until.to_rfc3339())
.bind(Utc::now().to_rfc3339())
.bind(id)
.execute(&self.pool)
.await?;
Ok(result.rows_affected() > 0)
}
pub async fn set_cloudflare_cooldown_until(
&self,
id: &str,
cooldown_until: DateTime<Utc>,
) -> AccountRepositoryResult<bool> {
let result = sqlx::query(
"update accounts set cloudflare_cooldown_until = ?, updated_at = ? where id = ?",
)
.bind(cooldown_until.to_rfc3339())
.bind(Utc::now().to_rfc3339())
.bind(id)
.execute(&self.pool)
.await?;
Ok(result.rows_affected() > 0)
}
fn account_from_row(
&self,
row: &sqlx::sqlite::SqliteRow,
+6
View File
@@ -174,6 +174,9 @@ pub(super) async fn apply_codex_account_error(
retry_after_seconds,
}) => {
let cooldown_until = Utc::now() + chrono::Duration::seconds(retry_after_seconds as i64);
let _ = repo
.set_quota_cooldown_until(&account.id, cooldown_until)
.await;
service
.account_pool
.lock()
@@ -183,6 +186,9 @@ pub(super) async fn apply_codex_account_error(
}
Some(CodexAccountErrorAction::CloudflareChallenge { cooldown_seconds }) => {
let cooldown_until = Utc::now() + chrono::Duration::seconds(cooldown_seconds as i64);
let _ = repo
.set_cloudflare_cooldown_until(&account.id, cooldown_until)
.await;
service
.account_pool
.lock()
+24
View File
@@ -451,6 +451,18 @@ async fn apply_upstream_account_retry_with_deps(
retry_after_seconds,
} => {
let cooldown_until = Utc::now() + Duration::seconds(retry_after_seconds as i64);
if let Some(repo) = deps.account_repository.as_ref() {
if repo
.set_quota_cooldown_until(&account.id, cooldown_until)
.await
.is_err()
{
tracing::warn!(
account_id = %account.id,
"failed to persist quota cooldown"
);
}
}
deps.account_pool
.lock()
.await
@@ -467,6 +479,18 @@ async fn apply_upstream_account_retry_with_deps(
}
UpstreamAccountRetry::CloudflareChallenge { cooldown_seconds } => {
let cooldown_until = Utc::now() + Duration::seconds(cooldown_seconds as i64);
if let Some(repo) = deps.account_repository.as_ref() {
if repo
.set_cloudflare_cooldown_until(&account.id, cooldown_until)
.await
.is_err()
{
tracing::warn!(
account_id = %account.id,
"failed to persist Cloudflare cooldown"
);
}
}
deps.account_pool
.lock()
.await