From 2ddcd7c936f5875710a4f783e2bded899fa13e2f Mon Sep 17 00:00:00 2001 From: Zyy Date: Fri, 4 Dec 2026 17:17:10 +0800 Subject: [PATCH] feat(openai): track turn rate limits per account and model Preserve session scheduling and retry hints while a model cools down. Show active model limits and their recovery times in the account list. Co-authored-by: Codex --- .../gateway-admin/src/model/accounts.rs | 4 + .../src/model/provider_credentials.rs | 1 + .../src/use_case/accounts/mod.rs | 2 + .../tests/use_case/account_groups.rs | 1 + .../tests/use_case/accounts/mod.rs | 1 + .../src/admin/accounts/presenter.rs | 13 + .../gateway-api/src/admin/accounts/wire.rs | 9 + .../crates/gateway-api/src/client_error.rs | 22 +- .../responses/websocket/protocol.rs | 3 + .../tests/admin/accounts/handlers.rs | 21 ++ .../crates/gateway-api/tests/admin/errors.rs | 1 + backend/crates/gateway-api/tests/admin/mod.rs | 2 + .../gateway-api/tests/data_plane/error.rs | 12 +- .../tests/data_plane/peer_link/mod.rs | 1 + backend/crates/gateway-core/src/error/mod.rs | 5 + .../src/provider_ports/cooldown.rs | 14 + .../provider_accounts/admin_adapter.rs | 34 +- .../provider_accounts/admin_queries.rs | 10 +- .../src/postgres/provider_accounts/runtime.rs | 34 ++ .../src/redis/credential_cooldown.rs | 348 ++++++++++++------ .../provider_accounts/admin_adapter.rs | 70 ++++ .../tests/postgres/provider_accounts/mod.rs | 1 + .../tests/redis/credential_cooldown.rs | 143 ++++++- .../providers/openai/src/credential/mod.rs | 2 +- .../openai/src/credential/quota/mod.rs | 2 +- .../openai/src/credential/selector.rs | 57 ++- .../providers/openai/src/provider/failure.rs | 57 ++- .../credential/contract/cooldown_reads.rs | 126 +++++++ .../openai/tests/provider/contract/mod.rs | 21 +- .../contract/model_account_eligibility.rs | 1 + .../crates/providers/openai/tests/support.rs | 18 +- docs/api.md | 10 +- docs/architecture.md | 6 + frontend/src/api/modules/accounts/types.ts | 1 + frontend/src/i18n/locales/en/accounts.json | 3 + .../src/i18n/locales/zh-Hans/accounts.json | 3 + .../src/i18n/locales/zh-Hant/accounts.json | 3 + .../components/account-status-badge/index.vue | 22 +- .../account-status-badge/presenter.ts | 27 +- .../account-status-badge/useStatusClock.ts | 2 +- .../accounts/composables/useAccountsQuery.ts | 11 + 41 files changed, 941 insertions(+), 183 deletions(-) diff --git a/backend/crates/gateway-admin/src/model/accounts.rs b/backend/crates/gateway-admin/src/model/accounts.rs index c0a2baf3b..77e45c9d8 100644 --- a/backend/crates/gateway-admin/src/model/accounts.rs +++ b/backend/crates/gateway-admin/src/model/accounts.rs @@ -92,6 +92,9 @@ pub struct AccountListQuery { /// Admin query service 从运行态存储取得的当前账号冷却快照 #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct AccountRuntimeSnapshot { + /// 模型维度冷却独立于整账号冷却,不能据此阻断其他模型 + pub model_cooldowns: + BTreeMap>, pub protection_rules: BTreeMap>, pub cooldown: BTreeMap, /// `None` 表示实时 lease 存储不可用;`Some` 中未出现的账号当前使用量为零 @@ -223,6 +226,7 @@ pub struct AccountPage { /// 同一状态快照下的账号事实与唯一状态投影 #[derive(Debug, Clone, PartialEq, Eq)] pub struct AccountPageItem { + pub model_cooldowns: Vec, pub platform_icon: Option, pub protection_rules: Vec, pub account: AccountRecord, diff --git a/backend/crates/gateway-admin/src/model/provider_credentials.rs b/backend/crates/gateway-admin/src/model/provider_credentials.rs index 8cf1734a5..38dd53827 100644 --- a/backend/crates/gateway-admin/src/model/provider_credentials.rs +++ b/backend/crates/gateway-admin/src/model/provider_credentials.rs @@ -1319,6 +1319,7 @@ impl fmt::Debug for ProviderExport { /// 统一账号目录的一行完整结果 #[derive(Debug, Clone, PartialEq)] pub struct AccountDirectoryItem { + pub model_cooldowns: Vec, pub platform_icon: Option, pub protection_rules: Vec, pub account: AccountRecord, diff --git a/backend/crates/gateway-admin/src/use_case/accounts/mod.rs b/backend/crates/gateway-admin/src/use_case/accounts/mod.rs index c67c7dc99..b35322f4d 100644 --- a/backend/crates/gateway-admin/src/use_case/accounts/mod.rs +++ b/backend/crates/gateway-admin/src/use_case/accounts/mod.rs @@ -513,6 +513,7 @@ impl DefaultAccountsService { lookback, ); Ok(AccountDirectoryItem { + model_cooldowns: stored.model_cooldowns, platform_icon: stored.platform_icon, protection_rules: stored.protection_rules, capacity: super::peer_link::upstream::account_capacity( @@ -648,6 +649,7 @@ impl AccountsService for DefaultAccountsService { lookback, ); Ok(AccountDirectoryItem { + model_cooldowns: item.model_cooldowns, platform_icon: item.platform_icon, protection_rules: item.protection_rules, capacity: crate::model::accounts::AccountCapacity { diff --git a/backend/crates/gateway-admin/tests/use_case/account_groups.rs b/backend/crates/gateway-admin/tests/use_case/account_groups.rs index f795d5996..42ca17692 100644 --- a/backend/crates/gateway-admin/tests/use_case/account_groups.rs +++ b/backend/crates/gateway-admin/tests/use_case/account_groups.rs @@ -315,6 +315,7 @@ impl AccountRuntimeStore for FakeRuntimeStore { ) -> AdminStoreResult { *self.requested_accounts.lock().expect("requested accounts") = account_ids.to_vec(); Ok(AccountRuntimeSnapshot { + model_cooldowns: BTreeMap::new(), protection_rules: std::collections::BTreeMap::new(), cooldown: BTreeMap::from([( "acct_limited".to_owned(), diff --git a/backend/crates/gateway-admin/tests/use_case/accounts/mod.rs b/backend/crates/gateway-admin/tests/use_case/accounts/mod.rs index 133dd6d93..c27d6b423 100644 --- a/backend/crates/gateway-admin/tests/use_case/accounts/mod.rs +++ b/backend/crates/gateway-admin/tests/use_case/accounts/mod.rs @@ -740,6 +740,7 @@ impl FakeAccountStore { last_error_message: account.last_error_message.clone(), }; AccountPageItem { + model_cooldowns: Vec::new(), platform_icon: None, protection_rules: Vec::new(), capacity: gateway_admin::model::accounts::AccountCapacity { diff --git a/backend/crates/gateway-api/src/admin/accounts/presenter.rs b/backend/crates/gateway-api/src/admin/accounts/presenter.rs index 54fc41c92..003ec49c3 100644 --- a/backend/crates/gateway-api/src/admin/accounts/presenter.rs +++ b/backend/crates/gateway-api/src/admin/accounts/presenter.rs @@ -67,6 +67,7 @@ pub(super) fn account_view( time: crate::time::TimePresenter, ) -> AccountView { let AccountDirectoryItem { + model_cooldowns, platform_icon, account, capacity, @@ -111,6 +112,17 @@ pub(super) fn account_view( .map(|value| super::upstream_info::view(value, now, time)); let (mut quota, refresh_token_expires_at) = account_quota_view(quota, cooldown, now, time); quota.protection_rules = protection_rules; + quota.model_rate_limits = model_cooldowns + .into_iter() + .map(|cooldown| { + let until: DateTime = cooldown.until().into(); + AccountModelRateLimitView { + model: cooldown.scope().value().to_owned(), + until: time.rfc3339(&until), + until_display: time.datetime(&until), + } + }) + .collect(); AccountView { platform_icon, quota_settings: account.quota_settings, @@ -271,6 +283,7 @@ pub(super) fn account_quota_view( let recovery_probe_required = cooldown.is_some_and(|value| value.kind.requires_probe()); ( AccountQuotaView { + model_rate_limits: Vec::new(), paused_windows: quota.paused_windows, refreshed_at: quota.observed_at.map(|value| time.rfc3339(&value)), refreshed_at_relative: quota diff --git a/backend/crates/gateway-api/src/admin/accounts/wire.rs b/backend/crates/gateway-api/src/admin/accounts/wire.rs index 53fca6721..cc2e9c6a6 100644 --- a/backend/crates/gateway-api/src/admin/accounts/wire.rs +++ b/backend/crates/gateway-api/src/admin/accounts/wire.rs @@ -382,6 +382,7 @@ pub use crate::admin::account_groups::AccountGroupRefView; #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct AccountQuotaView { + pub model_rate_limits: Vec, pub paused_windows: Vec, pub refreshed_at: Option, pub refreshed_at_relative: Option, @@ -397,6 +398,14 @@ pub struct AccountQuotaView { pub credits: Option, } +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct AccountModelRateLimitView { + pub model: String, + pub until: String, + pub until_display: String, +} + /// 上游点数安全视图,不透出额度响应中的其他 Provider 字段 #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] diff --git a/backend/crates/gateway-api/src/client_error.rs b/backend/crates/gateway-api/src/client_error.rs index 59e418cf0..a932a9387 100644 --- a/backend/crates/gateway-api/src/client_error.rs +++ b/backend/crates/gateway-api/src/client_error.rs @@ -119,22 +119,24 @@ pub fn gateway_error_response(error: &GatewayError) -> Response { } let mut response = (status, Json(body)).into_response(); response.extensions_mut().insert(error.clone()); - if let Some(delay) = error.retry_after() - && let Ok(value) = HeaderValue::from_str( - &delay - .as_secs() - .saturating_add(u64::from(delay.subsec_nanos() > 0)) - .max(1) - .to_string(), - ) - { + if let Some(seconds) = retry_after_seconds(error) { response .headers_mut() - .insert(axum::http::header::RETRY_AFTER, value); + .insert(axum::http::header::RETRY_AFTER, HeaderValue::from(seconds)); } response } +/// HTTP 和 WS 的等待秒数均向上取整,避免客户端在冷却到期前重新发起请求 +pub(crate) fn retry_after_seconds(error: &GatewayError) -> Option { + error.retry_after().map(|delay| { + delay + .as_secs() + .saturating_add(u64::from(delay.subsec_nanos() > 0)) + .max(1) + }) +} + /// Gateway 错误稳定映射,供 HTTP、SSE 和 WebSocket 共用 #[must_use] pub const fn gateway_error_contract( diff --git a/backend/crates/gateway-api/src/data_plane/responses/websocket/protocol.rs b/backend/crates/gateway-api/src/data_plane/responses/websocket/protocol.rs index 9340cdc4c..d7e722e74 100644 --- a/backend/crates/gateway-api/src/data_plane/responses/websocket/protocol.rs +++ b/backend/crates/gateway-api/src/data_plane/responses/websocket/protocol.rs @@ -185,6 +185,9 @@ pub(super) fn initial_engine_error_event( response_event_headers(response_headers) }; if upstream.is_none() { + if let Some(seconds) = crate::client_error::retry_after_seconds(&gateway) { + headers.insert("retry-after".to_owned(), Value::String(seconds.to_string())); + } // 会话 opening 身份不证明当前失败属于该请求;仅保留其他允许的会话头 for name in RESPONSE_REQUEST_ID_HEADERS { headers.remove(name); diff --git a/backend/crates/gateway-api/tests/admin/accounts/handlers.rs b/backend/crates/gateway-api/tests/admin/accounts/handlers.rs index a79f2eebc..4b7ff47fb 100644 --- a/backend/crates/gateway-api/tests/admin/accounts/handlers.rs +++ b/backend/crates/gateway-api/tests/admin/accounts/handlers.rs @@ -255,6 +255,18 @@ async fn account_cooldown_returns_an_instant_and_server_formatted_recovery() { last_error_message: None, }; let mut item = account_page_item(facts, now, "oauth"); + let model_until = now + Duration::seconds(8); + item.model_cooldowns = vec![gateway_core::provider_ports::ProviderScopedCooldown::new( + gateway_core::account::ProviderAccountId::new(item.account.id.clone()).unwrap(), + gateway_core::account::CredentialRevision::new( + item.account.credential_revision.get(), + ) + .unwrap(), + gateway_core::provider_ports::ProviderCooldownScope::upstream_model( + gateway_core::routing::UpstreamModelId::new("gpt-5.4").unwrap(), + ), + model_until.into(), + )]; if kind.is_error_protection() { item.protection_rules = vec![gateway_core::account::AccountProtectionMatch { id: "removed-rule".to_owned(), @@ -278,6 +290,14 @@ async fn account_cooldown_returns_an_instant_and_server_formatted_recovery() { let body = to_bytes(response.into_body(), 32768).await.unwrap(); let value: serde_json::Value = serde_json::from_slice(&body).unwrap(); let quota = &value["data"]["items"][0]["quota"]; + assert_eq!( + quota["modelRateLimits"], + serde_json::json!([{ + "model": "gpt-5.4", + "until": zone.local(model_until).to_rfc3339(), + "untilDisplay": zone.local(model_until).format("%Y-%m-%d %H:%M:%S").to_string(), + }]) + ); if kind.is_error_protection() { assert_eq!(quota["rateLimitReason"], "error_protection"); assert_eq!( @@ -411,6 +431,7 @@ fn account_page_item( use gateway_core::account::resolve_account_status; AccountPageItem { + model_cooldowns: Vec::new(), platform_icon: None, protection_rules: Vec::new(), account: AccountRecord { diff --git a/backend/crates/gateway-api/tests/admin/errors.rs b/backend/crates/gateway-api/tests/admin/errors.rs index 2f07680f8..171e608f8 100644 --- a/backend/crates/gateway-api/tests/admin/errors.rs +++ b/backend/crates/gateway-api/tests/admin/errors.rs @@ -564,6 +564,7 @@ mod provider { last_error_message: None, }; AccountPageItem { + model_cooldowns: Vec::new(), platform_icon: None, protection_rules: Vec::new(), capacity: gateway_admin::model::accounts::AccountCapacity { diff --git a/backend/crates/gateway-api/tests/admin/mod.rs b/backend/crates/gateway-api/tests/admin/mod.rs index a89c97d34..224bafa3f 100644 --- a/backend/crates/gateway-api/tests/admin/mod.rs +++ b/backend/crates/gateway-api/tests/admin/mod.rs @@ -1484,6 +1484,7 @@ impl AccountRuntimeStore for UnusedStore { async fn active_rate_limits(&self) -> AdminStoreResult { Ok(AccountRuntimeSnapshot { + model_cooldowns: BTreeMap::new(), cooldown: BTreeMap::new(), protection_rules: BTreeMap::new(), in_flight: Some(BTreeMap::new()), @@ -1492,6 +1493,7 @@ impl AccountRuntimeStore for UnusedStore { async fn account_runtime(&self, _: &[String]) -> AdminStoreResult { Ok(AccountRuntimeSnapshot { + model_cooldowns: BTreeMap::new(), cooldown: BTreeMap::new(), protection_rules: BTreeMap::new(), in_flight: Some(BTreeMap::new()), diff --git a/backend/crates/gateway-api/tests/data_plane/error.rs b/backend/crates/gateway-api/tests/data_plane/error.rs index d2628e223..f1b31db55 100644 --- a/backend/crates/gateway-api/tests/data_plane/error.rs +++ b/backend/crates/gateway-api/tests/data_plane/error.rs @@ -384,10 +384,14 @@ fn engine_provider_error_should_preserve_retry_classification() { #[test] fn local_provider_capacity_exhaustion_should_map_to_service_unavailable() { - let error = EngineError::Provider(ProviderError::new( - ProviderErrorKind::AccountCapacityUnavailable, - UpstreamSendState::NotSent, - )); + let error = EngineError::Provider( + ProviderError::new( + ProviderErrorKind::AccountCapacityUnavailable, + UpstreamSendState::NotSent, + ) + .with_retry_after(std::time::Duration::from_millis(7100)), + ); + assert_eq!(engine_error_response(&error).headers()["retry-after"], "8"); let projected = gateway_error_from_engine(&error); assert_eq!( diff --git a/backend/crates/gateway-api/tests/data_plane/peer_link/mod.rs b/backend/crates/gateway-api/tests/data_plane/peer_link/mod.rs index 1409c71f3..f69f7a641 100644 --- a/backend/crates/gateway-api/tests/data_plane/peer_link/mod.rs +++ b/backend/crates/gateway-api/tests/data_plane/peer_link/mod.rs @@ -91,6 +91,7 @@ async fn fixture(sharing: ClientPeerSharing, plaintext: &str) -> (AdminTestFixtu last_error_message: None, }; *fixture.account.lock().unwrap() = Some(AccountPageItem { + model_cooldowns: Vec::new(), platform_icon: None, protection_rules: Vec::new(), capacity: AccountCapacity { diff --git a/backend/crates/gateway-core/src/error/mod.rs b/backend/crates/gateway-core/src/error/mod.rs index 3482ec878..c00ce2e85 100644 --- a/backend/crates/gateway-core/src/error/mod.rs +++ b/backend/crates/gateway-core/src/error/mod.rs @@ -1586,6 +1586,11 @@ impl GatewayError { ), }; let mut gateway = gateway; + if gateway.kind() == GatewayErrorKind::AccountCapacityUnavailable + && let Some(delay) = error.retry_after() + { + gateway = gateway.with_retry_after(delay); + } if gateway.kind() == GatewayErrorKind::InvalidRequest && let Some(details) = error .failure_observation diff --git a/backend/crates/gateway-core/src/provider_ports/cooldown.rs b/backend/crates/gateway-core/src/provider_ports/cooldown.rs index 8226ce6e1..63a6f571e 100644 --- a/backend/crates/gateway-core/src/provider_ports/cooldown.rs +++ b/backend/crates/gateway-core/src/provider_ports/cooldown.rs @@ -113,6 +113,7 @@ pub struct ProviderScopedCooldown { credential_revision: CredentialRevision, scope: ProviderCooldownScope, until: SystemTime, + rate_limited_until: Option, } impl ProviderScopedCooldown { @@ -128,6 +129,7 @@ impl ProviderScopedCooldown { credential_revision, scope, until, + rate_limited_until: None, } } @@ -141,6 +143,18 @@ impl ProviderScopedCooldown { self.credential_revision } + /// 选号冷却可能还包含模型拒绝;目录只展示实际限流期限 + #[must_use] + pub fn with_rate_limited_until(mut self, until: Option) -> Self { + self.rate_limited_until = until.map(|until| until.min(self.until)); + self + } + + #[must_use] + pub const fn rate_limited_until(&self) -> Option { + self.rate_limited_until + } + #[must_use] pub const fn scope(&self) -> &ProviderCooldownScope { &self.scope diff --git a/backend/crates/gateway-store/src/postgres/provider_accounts/admin_adapter.rs b/backend/crates/gateway-store/src/postgres/provider_accounts/admin_adapter.rs index d864e541f..d7cfe60c6 100644 --- a/backend/crates/gateway-store/src/postgres/provider_accounts/admin_adapter.rs +++ b/backend/crates/gateway-store/src/postgres/provider_accounts/admin_adapter.rs @@ -325,19 +325,36 @@ impl AccountStore for PgAdminAccountStore { )); } let now = Utc::now(); - let cooldown = runtime.cooldown; let now_system_time = std::time::SystemTime::from(now); - let active_rate_limited_ids = cooldown + let active_rate_limited_ids = runtime + .cooldown .iter() .filter(|(_, cooldown)| cooldown.is_active(now_system_time)) .map(|(account_id, _)| account_id.clone()) .collect::>(); + let active_model_revisions = runtime + .model_cooldowns + .iter() + .flat_map(|(account, models)| { + models + .iter() + .filter(|model| model.until() > now_system_time) + .filter_map(|model| { + i64::try_from(model.credential_revision().get()) + .ok() + .map(|revision| (account.clone(), revision)) + }) + }) + .collect::>() + .into_iter() + .collect(); let page = load_admin_account_page( &self.pool, &self.query_budget, &query, now, active_rate_limited_ids, + active_model_revisions, ) .await?; let item_ids = page @@ -351,14 +368,12 @@ impl AccountStore for PgAdminAccountStore { .into_iter() .map(|(summary, platform_icon)| { let account_id = summary.id.clone(); - let projection = account_status_projection( - &summary, - now.into(), - cooldown.get(&account_id).copied(), - ); + let (projection, model_cooldowns) = + super::runtime::account_directory_projection(&summary, now.into(), &runtime); let mut account = admin_account_record(summary)?; account.groups = groups_by_account.remove(&account_id).unwrap_or_default(); Ok(AccountPageItem { + model_cooldowns, platform_icon, protection_rules: runtime .protection_rules @@ -397,8 +412,8 @@ impl AccountStore for PgAdminAccountStore { return Ok(None); }; let now = Utc::now(); - let cooldown = runtime.cooldown.get(account_id).copied(); - let projection = account_status_projection(&record.summary, now.into(), cooldown); + let (projection, model_cooldowns) = + super::runtime::account_directory_projection(&record.summary, now.into(), &runtime); let account_id = record.summary.id.clone(); let mut groups = self .account_groups_by_account(std::slice::from_ref(&account_id)) @@ -424,6 +439,7 @@ impl AccountStore for PgAdminAccountStore { ) })?; Ok(Some(AccountPageItem { + model_cooldowns, platform_icon, protection_rules: runtime .protection_rules diff --git a/backend/crates/gateway-store/src/postgres/provider_accounts/admin_queries.rs b/backend/crates/gateway-store/src/postgres/provider_accounts/admin_queries.rs index adee4efc0..bbd910acb 100644 --- a/backend/crates/gateway-store/src/postgres/provider_accounts/admin_queries.rs +++ b/backend/crates/gateway-store/src/postgres/provider_accounts/admin_queries.rs @@ -16,7 +16,10 @@ pub(crate) async fn load_admin_account_page( query: &AdminAccountListQuery, now: DateTime, active_rate_limited_ids: Vec, + active_model_revisions: Vec<(String, i64)>, ) -> AdminStoreResult { + let (model_account_ids, model_revisions): (Vec<_>, Vec<_>) = + active_model_revisions.into_iter().unzip(); validate_group_filter(pool, query.group_filter.as_ref()).await?; let provider_kind = query @@ -56,7 +59,10 @@ pub(crate) async fn load_admin_account_page( when a.credential_state <> 'ready' or a.access_token_expires_at <= $2 then 'error' when a.quota_access_state = 'exhausted' then 'quota_exhausted' - when a.id = any($1::text[]) then 'rate_limited' + when a.id = any($1::text[]) or exists ( + select 1 from unnest($13::text[], $14::bigint[]) as limits(account_id, revision) + where limits.account_id = a.id and limits.revision = a.credential_revision + ) then 'rate_limited' when a.quota_paused then 'quota_paused' else 'normal' end as admin_status @@ -165,6 +171,8 @@ pub(crate) async fn load_admin_account_page( .bind(usage_ids) .bind(usage_tokens) .bind(usage_latest) + .bind(model_account_ids) + .bind(model_revisions) .fetch_all(&mut *snapshot.tx) .await .map_err(|source| postgres_unavailable("load admin account page", source))?; diff --git a/backend/crates/gateway-store/src/postgres/provider_accounts/runtime.rs b/backend/crates/gateway-store/src/postgres/provider_accounts/runtime.rs index b7e1a3ffd..1be66172a 100644 --- a/backend/crates/gateway-store/src/postgres/provider_accounts/runtime.rs +++ b/backend/crates/gateway-store/src/postgres/provider_accounts/runtime.rs @@ -12,6 +12,40 @@ use gateway_core::{ use super::ProviderAccountSummary; +pub(crate) fn account_directory_projection( + account: &ProviderAccountSummary, + now: SystemTime, + runtime: &gateway_admin::model::accounts::AccountRuntimeSnapshot, +) -> ( + AccountStatusProjection, + Vec, +) { + let models = runtime + .model_cooldowns + .get(&account.id) + .into_iter() + .flatten() + .filter(|cooldown| { + cooldown.credential_revision().get() == account.credential_revision.get() + && cooldown.until() > now + }) + .cloned() + .collect::>(); + let mut projection = + account_status_projection(account, now, runtime.cooldown.get(&account.id).copied()); + // 目录汇总展示部分模型受限,不将其写入整账号 cooldown 或用于其他模型的调度 + if !models.is_empty() + && matches!( + projection.status, + gateway_core::account::AccountStatus::Normal + | gateway_core::account::AccountStatus::QuotaPaused + ) + { + projection.status = gateway_core::account::AccountStatus::RateLimited; + } + (projection, models) +} + #[must_use] pub(crate) fn account_status_projection( account: &ProviderAccountSummary, diff --git a/backend/crates/gateway-store/src/redis/credential_cooldown.rs b/backend/crates/gateway-store/src/redis/credential_cooldown.rs index 6a1f6aa9b..d52a4cf18 100644 --- a/backend/crates/gateway-store/src/redis/credential_cooldown.rs +++ b/backend/crates/gateway-store/src/redis/credential_cooldown.rs @@ -11,7 +11,7 @@ use gateway_core::{ }, }; use redis::{Script, aio::ConnectionManager}; -use std::{collections::BTreeMap, sync::LazyLock}; +use std::{collections::BTreeMap, sync::LazyLock, time::SystemTime}; use crate::{Revision, StoreError, StoreResult, redis_unavailable, require_nonempty}; @@ -55,6 +55,42 @@ static WRITE_ACCOUNT: LazyLock