mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-20 08:01:46 +00:00
* fix(retry): rotate accounts after connection failures Reliable NotSent HTTP connection failures stay on the same account and egress until the shared 30-second opening window is exhausted. When an account's proxy does not respond, each opening waits for the full 15-second connect timeout on the same route, so requests that selected that account fail after about 30 seconds while other accounts stay idle. After a connection failure, exclude the account and reselect through the existing rotation path, consuming routing and rotation budget and sharing the same opening window. Rotation happens after the first failure because a second same-account opening on an unresponsive egress would leave too little of the window to rotate. If no other account is eligible, restore the exclusion and continue the original same-account HTTP recovery, so single-account deployments keep their current behavior. Required accounts and continuation attempts are unchanged. Refs #485 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(retry): retain same-account connection recovery Keep existing account rotation rules and cover rotation after a recovered connection receives a replay-safe rate-limit rejection. Co-authored-by: Codex <noreply@openai.com> * fix(core): reserve connection budget for account rotation Retry the original account before rotating on unsent connection failures. Split the retry window and retain an opening for HTTP recovery on another account, with bounded fallback when no alternative can be selected. Co-authored-by: Codex <noreply@openai.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Zyy <zyycn@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com>
88 lines
3.2 KiB
Rust
88 lines
3.2 KiB
Rust
//! 验证各传输共享建连恢复预算,且不限制成功建连后的执行
|
|
|
|
use gateway_core::engine::connection::ConnectionBudget;
|
|
|
|
#[test]
|
|
fn elapsed_recovery_window_is_shared_without_waiting_for_real_time() {
|
|
use std::{
|
|
cell::Cell,
|
|
time::{Duration, Instant},
|
|
};
|
|
thread_local! {
|
|
static NOW: Cell<Instant> = Cell::new(Instant::now());
|
|
}
|
|
fn now() -> Instant {
|
|
NOW.get()
|
|
}
|
|
let request = ConnectionBudget::with_clock(now);
|
|
let fallback = request.clone();
|
|
assert_eq!(request.begin().unwrap(), Some(Duration::from_secs(30)));
|
|
NOW.set(now() + Duration::from_secs(29));
|
|
assert_eq!(fallback.begin().unwrap(), Some(Duration::from_secs(1)));
|
|
assert_eq!(request.startup_remaining(), Some(Duration::from_secs(1)));
|
|
NOW.set(now() + Duration::from_secs(1));
|
|
assert!(request.exhausted());
|
|
assert!(fallback.begin().is_err());
|
|
assert!(request.remaining().is_none());
|
|
assert_eq!(fallback.startup_remaining(), Some(Duration::ZERO));
|
|
}
|
|
|
|
#[test]
|
|
fn same_account_retry_preserves_time_and_an_opening_for_rotation() {
|
|
use std::{
|
|
cell::Cell,
|
|
time::{Duration, Instant},
|
|
};
|
|
thread_local! {
|
|
static NOW: Cell<Instant> = Cell::new(Instant::now());
|
|
}
|
|
fn now() -> Instant {
|
|
NOW.get()
|
|
}
|
|
let request = ConnectionBudget::with_clock(now);
|
|
// 后台 WS 先占一次,HTTP 首次建连再占一次,模拟代理持续不响应
|
|
let background = request.clone();
|
|
assert_eq!(background.begin().unwrap(), Some(Duration::from_secs(30)));
|
|
NOW.set(now() + Duration::from_secs(1));
|
|
assert_eq!(request.begin().unwrap(), Some(Duration::from_secs(29)));
|
|
NOW.set(now() + Duration::from_secs(15));
|
|
assert_eq!(request.remaining_openings(), 2);
|
|
let retry = request.clone().reserve_next_opening();
|
|
assert_eq!(retry.begin().unwrap(), Some(Duration::from_secs(7)));
|
|
assert_eq!(request.remaining_openings(), 1);
|
|
assert!(retry.begin().is_err());
|
|
assert!(!request.exhausted());
|
|
NOW.set(now() + Duration::from_secs(7));
|
|
// 重选使用原预算,既不继承 A 的局部时间限制,也不能重置总次数
|
|
assert_eq!(request.begin().unwrap(), Some(Duration::from_secs(7)));
|
|
assert!(request.exhausted());
|
|
assert!(background.begin().is_err());
|
|
request.complete();
|
|
assert_eq!(retry.begin().unwrap(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn all_transports_share_one_opening_budget_but_other_requests_do_not() {
|
|
let request = ConnectionBudget::default();
|
|
let http = request.clone();
|
|
let websocket = request.clone();
|
|
for transport in [&http, &websocket, &http, &websocket] {
|
|
assert!(transport.begin().unwrap().is_some());
|
|
}
|
|
assert!(request.exhausted());
|
|
assert!(http.begin().is_err());
|
|
assert!(websocket.retry_delay(0, "req_shared").is_none());
|
|
assert!(ConnectionBudget::default().begin().is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn successful_opening_does_not_limit_later_generation_or_protocol_recovery() {
|
|
let request = ConnectionBudget::default();
|
|
request.begin().unwrap();
|
|
request.complete();
|
|
assert!(request.startup_remaining().is_none());
|
|
assert!(request.retry_delay(0, "req_sent").is_none());
|
|
assert!(!request.exhausted());
|
|
assert!(request.begin().unwrap().is_none());
|
|
}
|