Files
codex-proxy-rs/backend/src/dispatch/affinity/service.rs
T
zyycnandCodex 68d80d99f3 fix: harden response recovery and resource bounds
Keep replay history within the active downstream WebSocket, rotate failed requests through the configured account scheduler, and retain only bounded response ownership metadata in Redis.\n\nRemove duplicate account retries, bound transport waits and runtime maps, defer noncritical stream persistence, and cover the recovery and timeout paths with integration tests.

Co-authored-by: Codex <noreply@openai.com>
2026-07-13 04:40:31 +08:00

115 lines
3.3 KiB
Rust

//! 运行时会话亲和服务。
use chrono::{DateTime, Duration, Utc};
use thiserror::Error;
use super::{
store::{RedisSessionAffinityStore, RedisSessionAffinityStoreError},
types::SessionAffinityEntry,
};
const DEFAULT_SESSION_AFFINITY_TTL_SECS: i64 = 4 * 60 * 60;
/// 运行时会话亲和性服务。
#[derive(Clone)]
pub struct SessionAffinityService {
store: RedisSessionAffinityStore,
ttl: Duration,
}
/// 运行时会话亲和性错误。
#[derive(Debug, Error)]
pub enum SessionAffinityError {
#[error("session affinity store error: {0}")]
Store(#[from] RedisSessionAffinityStoreError),
}
impl SessionAffinityService {
pub fn new(store: RedisSessionAffinityStore) -> Self {
let ttl = Duration::seconds(DEFAULT_SESSION_AFFINITY_TTL_SECS);
Self { store, ttl }
}
pub async fn record(
&self,
response_id: String,
entry: SessionAffinityEntry,
) -> Result<(), SessionAffinityError> {
Ok(self.store.upsert(&response_id, &entry, self.ttl).await?)
}
pub async fn lookup(
&self,
response_id: &str,
now: DateTime<Utc>,
) -> Option<SessionAffinityEntry> {
self.entry(response_id, now).await
}
pub async fn lookup_latest_account_by_conversation(
&self,
conversation_id: &str,
max_age: Option<Duration>,
variant_hash: Option<&str>,
now: DateTime<Utc>,
) -> Option<String> {
self.latest(conversation_id, max_age, variant_hash, now)
.await
.map(|(_, entry)| entry.account_id)
}
pub async fn forget(&self, response_id: &str) -> bool {
match self.store.forget(response_id).await {
Ok(forgotten) => forgotten,
Err(error) => {
tracing::warn!(
response_id,
error = %error,
"failed to remove Redis session affinity"
);
false
}
}
}
pub async fn forget_account(&self, account_id: &str) -> bool {
match self.store.forget_account(account_id).await {
Ok(forgotten) => forgotten > 0,
Err(error) => {
tracing::warn!(account_id, error = %error, "failed to remove account affinities");
false
}
}
}
async fn entry(&self, response_id: &str, now: DateTime<Utc>) -> Option<SessionAffinityEntry> {
match self.store.get(response_id, now, self.ttl).await {
Ok(entry) => entry,
Err(error) => {
tracing::warn!(response_id, error = %error, "failed to read Redis session affinity");
None
}
}
}
async fn latest(
&self,
conversation_id: &str,
max_age: Option<Duration>,
variant_hash: Option<&str>,
now: DateTime<Utc>,
) -> Option<(String, SessionAffinityEntry)> {
match self
.store
.latest_by_conversation(conversation_id, max_age, variant_hash, now, self.ttl)
.await
{
Ok(entry) => entry,
Err(error) => {
tracing::warn!(conversation_id, error = %error, "failed to query Redis affinity index");
None
}
}
}
}