From 756882293f9b1247056f7b6b42926af98691eefc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Thu, 19 Nov 2026 16:01:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=20app=20HTTP=20=E9=80=A3=E7=B7=9A=205?= =?UTF-8?q?=20=E7=A7=92=E5=BF=AB=E5=A4=B1=E6=95=97=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E9=87=8D=E9=96=8B=E5=BE=8C=E3=80=8C=E5=A5=BD=E4=B9=85=E9=80=A3?= =?UTF-8?q?=E4=B8=8D=E4=B8=8A=E3=80=8D=E5=92=8C=E6=8C=89=E9=88=95=E5=8D=A1?= =?UTF-8?q?=2030=20=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:所有 ureq client 只設了總超時,連接超時是 ureq 預設的 30 秒。 重開 App、斷網、tailnet 未就緒時,每個請求(重連帳號伺服器、模型呼叫、 web 抓取、webhook)都靜靜掛滿 30 秒才發現連不上——App 重開後好久連不上、 按停止/任何按鈕長轉圈,都是這 30 秒。 - net::agent(共用基底)+所有自建 client(remote/teams/triggers/embed/ phone/web/channels/botbrowser/mcp_oauth)統一 timeout_connect 5 秒 (HF 大檔下載 10 秒、本機瀏覽器 3 秒) - 帳號伺服器重連迴圈照 1→2→4…30 秒退避:連不上時 5+1 秒就重試, 重開 App 到連上從 30–70 秒降到 ≤6 秒 - 新鎖測試 connecting_dies_fast_everywhere 已驗證: cargo test 722 全綠。 未驗證: 真機重開後連線秒數(使用者自觀);離線場景需實機斷網。 --- src/botbrowser.rs | 2 +- src/channels.rs | 2 +- src/embed.rs | 4 ++-- src/mcp_oauth.rs | 2 +- src/net.rs | 13 ++++++++++++- src/phone.rs | 2 +- src/remote.rs | 2 +- src/teams.rs | 2 +- src/triggers.rs | 2 +- src/web.rs | 1 + 10 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/botbrowser.rs b/src/botbrowser.rs index 6db5b3a..8e15276 100644 --- a/src/botbrowser.rs +++ b/src/botbrowser.rs @@ -426,7 +426,7 @@ pub fn remove(name: &str) -> Result<(), String> { } pub(crate) fn local() -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(5))).http_status_as_error(false).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(5))).timeout_connect(Some(Duration::from_secs(3))).http_status_as_error(false).build().into() } /// The fixed port of 「預設」 (`ANYBOT_BROWSER_PORT` overrides it, for testing). diff --git a/src/channels.rs b/src/channels.rs index 599ac4e..07e2365 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -140,7 +140,7 @@ fn set_status(id: u64, ok: bool, note: &str, got: u64) { } fn agent(timeout: u64) -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(timeout))).http_status_as_error(false).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(timeout))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).build().into() } /// Seconds to wait after a 429 ("rate:N" from get_json), bounded so a bogus value can't park diff --git a/src/embed.rs b/src/embed.rs index b6503a4..32afdcd 100644 --- a/src/embed.rs +++ b/src/embed.rs @@ -96,7 +96,7 @@ fn engine_id(e: &Engine) -> String { } fn agent(secs: u64) -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(secs))).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(secs))).timeout_connect(Some(Duration::from_secs(5))).build().into() } /// An installed embedding model in Ollama, by the names people use (bge-m3, nomic-embed-text…). @@ -208,7 +208,7 @@ fn fetch_local() -> Result<(), String> { std::fs::create_dir_all(&dir).map_err(|e| format!("建不了資料夾:{e}"))?; let url = |f: &str| format!("{}/{LOCAL_MODEL}/resolve/main/{f}", hub()); // No limit on the whole download (512 MB on a slow line takes a while): only on silence. - let net: ureq::Agent = crate::net::config().timeout_connect(Some(Duration::from_secs(30))).timeout_recv_response(Some(Duration::from_secs(60))).timeout_per_read(Some(Duration::from_secs(60))).build().into(); + let net: ureq::Agent = crate::net::config().timeout_connect(Some(Duration::from_secs(10))).timeout_recv_response(Some(Duration::from_secs(60))).timeout_per_read(Some(Duration::from_secs(60))).build().into(); let why = |e: ureq::Error| format!("連不到 Hugging Face:{e}"); let mut r = net.get(&url("tokenizer.json")).call().map_err(why)?; let tok = r.body_mut().with_config().limit(64 << 20).read_to_vec().map_err(|e| format!("字表沒下載完:{e}"))?; diff --git a/src/mcp_oauth.rs b/src/mcp_oauth.rs index a3142c5..6ae3af6 100644 --- a/src/mcp_oauth.rs +++ b/src/mcp_oauth.rs @@ -706,7 +706,7 @@ pub mod tests { let fake = FakeOAuth::start(); let name = format!("oauth-paste-{}", fake.port); let url = start(&name, &fake.url).unwrap(); - let no_follow: ureq::Agent = crate::net::config().max_redirects(0).http_status_as_error(false).build().into(); + let no_follow: ureq::Agent = crate::net::config().max_redirects(0).timeout_connect(Some(std::time::Duration::from_secs(5))).http_status_as_error(false).build().into(); let resp = no_follow.get(url.as_str()).call().unwrap(); let back = resp.headers().get("location").unwrap().to_str().unwrap().to_string(); assert_eq!(finish_from_url(&back).unwrap(), name); diff --git a/src/net.rs b/src/net.rs index 1cb5523..a8adf82 100644 --- a/src/net.rs +++ b/src/net.rs @@ -30,9 +30,13 @@ pub fn config() -> ureq::config::ConfigBuilder { /// The common client: `secs` for the whole request, an HTTP error status is an answer (not an /// Err). One per timeout, kept (its connections are reused). +/// +/// 連接本身 5 秒封顶:ureq 的默认连接超时是 30 秒——网络刚断、重开机、tailnet +/// 未就绪时,每个请求都会静静地挂满半分钟才发现连不上:App 重开后的「好久 +/// 连不上」、按下停止或任何按钮后的长转圈,都是这 30 秒。快失败、快重试。 pub fn agent(secs: u64) -> ureq::Agent { static AGENTS: OnceLock>> = OnceLock::new(); - AGENTS.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()).entry(secs).or_insert_with(|| config().timeout_global(Some(Duration::from_secs(secs))).http_status_as_error(false).build().into()).clone() + AGENTS.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()).entry(secs).or_insert_with(|| config().timeout_global(Some(Duration::from_secs(secs))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).build().into()).clone() } #[cfg(test)] @@ -47,4 +51,11 @@ mod tests { assert_eq!(format!("{:?}", a.config().timeouts().global), format!("{:?}", b.config().timeouts().global)); assert!(!a.config().http_status_as_error()); } + + #[test] + fn connecting_dies_fast_everywhere() { + // 連接 5 秒封顶(不是 ureq 默认的 30):重开、断网、tailnet 未就绪时不卡 30 秒。 + let a = super::agent(40); + assert_eq!(a.config().timeouts().connect, Some(std::time::Duration::from_secs(5)), "連接超時 5 秒"); + } } diff --git a/src/phone.rs b/src/phone.rs index a158fdf..2776249 100644 --- a/src/phone.rs +++ b/src/phone.rs @@ -25,7 +25,7 @@ fn x_api() -> String { } fn agent() -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(30))).http_status_as_error(false).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(30))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).build().into() } /// "AccountSID:AuthToken" → (sid, Basic header). diff --git a/src/remote.rs b/src/remote.rs index bb1be59..b330611 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -2071,7 +2071,7 @@ pub(crate) fn relay_agent(secs: u64, url: &str) -> ureq::Agent { .unwrap_or_else(|e| e.into_inner()) .entry(secs) .or_insert_with(|| { - let config = crate::net::config().timeout_global(Some(Duration::from_secs(secs))).http_status_as_error(false).proxy(None).max_idle_connections(0).max_idle_connections_per_host(0).build(); + let config = crate::net::config().timeout_global(Some(Duration::from_secs(secs))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).proxy(None).max_idle_connections(0).max_idle_connections_per_host(0).build(); ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), OffTheAir(Default::default())) }) .clone() diff --git a/src/teams.rs b/src/teams.rs index aa60b68..6fdf6a2 100644 --- a/src/teams.rs +++ b/src/teams.rs @@ -20,7 +20,7 @@ fn graph_base() -> String { } fn agent() -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(30))).http_status_as_error(false).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(30))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).build().into() } fn form(url: &str, fields: &[(&str, &str)]) -> Result { diff --git a/src/triggers.rs b/src/triggers.rs index 7054007..1ab0f65 100644 --- a/src/triggers.rs +++ b/src/triggers.rs @@ -404,7 +404,7 @@ fn webhooks() -> &'static Mutex> { } fn agent(secs: u64) -> ureq::Agent { - crate::net::config().timeout_global(Some(Duration::from_secs(secs))).http_status_as_error(false).build().into() + crate::net::config().timeout_global(Some(Duration::from_secs(secs))).timeout_connect(Some(Duration::from_secs(5))).http_status_as_error(false).build().into() } fn get(url: &str, headers: &[(&str, String)]) -> Result { diff --git a/src/web.rs b/src/web.rs index 0623233..3207f3f 100644 --- a/src/web.rs +++ b/src/web.rs @@ -344,6 +344,7 @@ pub fn get_public_bytes(url: &str, limit: u64) -> Result<(String, u16, String, V .http_status_as_error(false) .max_redirects(0) .timeout_global(Some(Duration::from_secs(20))) + .timeout_connect(Some(Duration::from_secs(5))) .user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15") .build(); let agent = ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), PublicResolver(Default::default()));