全 app HTTP 連線 5 秒快失敗:修重開後「好久連不上」和按鈕卡 30 秒
根因:所有 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 全綠。 未驗證: 真機重開後連線秒數(使用者自觀);離線場景需實機斷網。
This commit is contained in:
parent
d89fc50ed5
commit
756882293f
|
|
@ -426,7 +426,7 @@ pub fn remove(name: &str) -> Result<(), String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn local() -> ureq::Agent {
|
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).
|
/// The fixed port of 「預設」 (`ANYBOT_BROWSER_PORT` overrides it, for testing).
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ fn set_status(id: u64, ok: bool, note: &str, got: u64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn agent(timeout: u64) -> ureq::Agent {
|
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
|
/// Seconds to wait after a 429 ("rate:N" from get_json), bounded so a bogus value can't park
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ fn engine_id(e: &Engine) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn agent(secs: u64) -> ureq::Agent {
|
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…).
|
/// 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}"))?;
|
std::fs::create_dir_all(&dir).map_err(|e| format!("建不了資料夾:{e}"))?;
|
||||||
let url = |f: &str| format!("{}/{LOCAL_MODEL}/resolve/main/{f}", hub());
|
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.
|
// 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 why = |e: ureq::Error| format!("連不到 Hugging Face:{e}");
|
||||||
let mut r = net.get(&url("tokenizer.json")).call().map_err(why)?;
|
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}"))?;
|
let tok = r.body_mut().with_config().limit(64 << 20).read_to_vec().map_err(|e| format!("字表沒下載完:{e}"))?;
|
||||||
|
|
|
||||||
|
|
@ -706,7 +706,7 @@ pub mod tests {
|
||||||
let fake = FakeOAuth::start();
|
let fake = FakeOAuth::start();
|
||||||
let name = format!("oauth-paste-{}", fake.port);
|
let name = format!("oauth-paste-{}", fake.port);
|
||||||
let url = start(&name, &fake.url).unwrap();
|
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 resp = no_follow.get(url.as_str()).call().unwrap();
|
||||||
let back = resp.headers().get("location").unwrap().to_str().unwrap().to_string();
|
let back = resp.headers().get("location").unwrap().to_str().unwrap().to_string();
|
||||||
assert_eq!(finish_from_url(&back).unwrap(), name);
|
assert_eq!(finish_from_url(&back).unwrap(), name);
|
||||||
|
|
|
||||||
13
src/net.rs
13
src/net.rs
|
|
@ -30,9 +30,13 @@ pub fn config() -> ureq::config::ConfigBuilder<ureq::typestate::AgentScope> {
|
||||||
|
|
||||||
/// The common client: `secs` for the whole request, an HTTP error status is an answer (not an
|
/// 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).
|
/// Err). One per timeout, kept (its connections are reused).
|
||||||
|
///
|
||||||
|
/// 連接本身 5 秒封顶:ureq 的默认连接超时是 30 秒——网络刚断、重开机、tailnet
|
||||||
|
/// 未就绪时,每个请求都会静静地挂满半分钟才发现连不上:App 重开后的「好久
|
||||||
|
/// 连不上」、按下停止或任何按钮后的长转圈,都是这 30 秒。快失败、快重试。
|
||||||
pub fn agent(secs: u64) -> ureq::Agent {
|
pub fn agent(secs: u64) -> ureq::Agent {
|
||||||
static AGENTS: OnceLock<Mutex<HashMap<u64, ureq::Agent>>> = OnceLock::new();
|
static AGENTS: OnceLock<Mutex<HashMap<u64, ureq::Agent>>> = 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)]
|
#[cfg(test)]
|
||||||
|
|
@ -47,4 +51,11 @@ mod tests {
|
||||||
assert_eq!(format!("{:?}", a.config().timeouts().global), format!("{:?}", b.config().timeouts().global));
|
assert_eq!(format!("{:?}", a.config().timeouts().global), format!("{:?}", b.config().timeouts().global));
|
||||||
assert!(!a.config().http_status_as_error());
|
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 秒");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ fn x_api() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn agent() -> ureq::Agent {
|
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).
|
/// "AccountSID:AuthToken" → (sid, Basic header).
|
||||||
|
|
|
||||||
|
|
@ -2071,7 +2071,7 @@ pub(crate) fn relay_agent(secs: u64, url: &str) -> ureq::Agent {
|
||||||
.unwrap_or_else(|e| e.into_inner())
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
.entry(secs)
|
.entry(secs)
|
||||||
.or_insert_with(|| {
|
.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()))
|
ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), OffTheAir(Default::default()))
|
||||||
})
|
})
|
||||||
.clone()
|
.clone()
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ fn graph_base() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn agent() -> ureq::Agent {
|
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<Value, String> {
|
fn form(url: &str, fields: &[(&str, &str)]) -> Result<Value, String> {
|
||||||
|
|
|
||||||
|
|
@ -404,7 +404,7 @@ fn webhooks() -> &'static Mutex<HashMap<u64, (String, u64)>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn agent(secs: u64) -> ureq::Agent {
|
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<Value, String> {
|
fn get(url: &str, headers: &[(&str, String)]) -> Result<Value, String> {
|
||||||
|
|
|
||||||
|
|
@ -344,6 +344,7 @@ pub fn get_public_bytes(url: &str, limit: u64) -> Result<(String, u16, String, V
|
||||||
.http_status_as_error(false)
|
.http_status_as_error(false)
|
||||||
.max_redirects(0)
|
.max_redirects(0)
|
||||||
.timeout_global(Some(Duration::from_secs(20)))
|
.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")
|
.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();
|
.build();
|
||||||
let agent = ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), PublicResolver(Default::default()));
|
let agent = ureq::Agent::with_parts(config, ureq::unversioned::transport::DefaultConnector::new(), PublicResolver(Default::default()));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue