每個回應不再多等 40 毫秒:帳號伺服器(ai,家裡)上一個最簡單的 /api/hello,在同一條連線上每個要 40–50 毫秒——直接打 relay 容器也一樣,不是 Caddy 也不是網路。httpd 把回應的標頭和內容分兩次寫,一般的 HTTP 連線又沒有關掉 Nagle(只有 WebSocket 關了):內容要等對方確認標頭才送,對方的確認壓著最多 40 毫秒。帳號伺服器、手機頁面都用這份 httpd,手機經帳號伺服器問大腦一次要付兩次。現在每條連線一接進來就關掉 Nagle,16 KB 以下的回應標頭和內容一次寫出;手機頁面的區網 HTTPS 那層(lantls)兩邊的連線也關掉。
已驗證: cargo test 全綠(822)、relay cargo test 全綠(26);新測試 answers_on_a_kept_connection_come_back_at_once(同一條連線 10 個請求平均要在 15 毫秒內;macOS 的本機迴路不壓確認,這台關掉修正也會過——擋的是 Linux,帳號伺服器和大腦都跑在 Linux);clippy 錯誤數跟之前一樣(12)。 未驗證: Linux 上修正前後的對照,部署到 ai 之後量(修正前 /api/hello 每個 50 毫秒)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
parent
63187a7a19
commit
f6971861d2
|
|
@ -536,6 +536,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。
|
|||
- **更新要驗簽章**:`make sign-setup`(作者做一次)讓之後每個 commit 用 SSH 金鑰簽章,公鑰記在 `deploy/allowed_signers`。安裝時把它記到原始碼資料夾外面(`~/.config/anybot/allowed_signers`);之後 `make update`/重跑安裝腳本,新版不是那把金鑰簽的就不裝(Mac、Linux、Windows 都一樣)。換金鑰:用舊金鑰簽一個把新公鑰加進名單的 commit。確定要跳過一次:`ANYBOT_UNSIGNED=1`。還沒有 `deploy/allowed_signers` 時跟以前一樣不檢查。
|
||||
- **聊天各存各的檔**:state.json 只剩設定;每個 Bot 的聊天存在 `chats/bot-<編號>.json`,有變才寫;過去的對話(討論串)另外存在 `chats/threads/bot-<編號>.json`,只有收起、切換或刪掉對話時才寫。Bot 工作時每 5 秒的存檔只寫目前這段對話:一個留了 3.5 萬則舊訊息(60 MB)的 Bot,存一次從 90–290 毫秒、寫 77 MB 降到 7–10 毫秒、寫 0.75 MB(以前每 5 秒整份重寫,在 2 核、沒有顯示卡的電腦上視窗每次停住快一秒)。刪掉 Bot 時兩個檔一起刪。舊版存的(聊天在 state.json 裡、或過去的對話在聊天檔裡)照樣讀得進來,下次存檔就分開;中間跑過舊版 AnyBot(它看不到另存的對話、放下的新對話寫在聊天檔裡)也不會少:兩邊合在一起,舊版放下的排前面。壞掉的檔案改名留著,不會被蓋掉。
|
||||
- **沒有顯示卡的電腦**(虛擬機、透過遠端桌面看的電腦:畫面由 CPU 畫,例如 llvmpipe):開視窗時認出來、紀錄寫一行。Bot 工作時的轉圈、流光字、側欄頭像、串流的重畫從每秒 20–30 次放慢成每秒 4 次——每一張都是整個視窗用 CPU 重畫,原本吃掉大半個核心、旁邊 Bot 自己開的瀏覽器跟著卡,從遠端桌面看視窗一直閃。有顯示卡的電腦照舊。
|
||||
- **回應不再每個多等 40 毫秒**:帳號伺服器和手機頁面(含區網 HTTPS 那一層)每條連線都關掉 Nagle,小的回應標頭和內容一次寫出。以前標頭、內容分兩次寫,內容要等對方確認標頭,對方又把確認壓著最多 40 毫秒:同一條連線上每個回應 40–50 毫秒(家裡實測帳號伺服器的 /api/hello),手機經帳號伺服器問大腦一次要付兩次。
|
||||
- **帳號伺服器的資源**:閒置的連線從每秒醒 25 次降到 5 次;每條連線的讀取緩衝從 128 KB 降到 32 KB;請求的開頭 15 秒內沒送完就斷(原本最長 90 秒)。本機壓測(100 台電腦連著、3000 個請求、120 次錯誤登入、128 個送一半的請求、3 輪重連):記憶體 32 MB 持平,執行緒和檔案描述元都回到起點,閒置 CPU 約 1.5%(一個核心)。
|
||||
- **團隊成員在 Bot 忙的時候傳的訊息**不再被丟掉:排在後面,Bot 做完手上的事就輪到它(每個 Bot 最多排 8 則)。成員的話不會插進擁有者正在跑的那一輪。
|
||||
- **群組沒有 @ 時看擅長分派**:訊息的內容對得上某些成員「擅長」寫的事(而且不在它「不找它做的事」裡),就只由那幾位回答,聊天裡會有一行「這題看擅長交給「…」。要其他人也回答,用 @ 點名。」;對不上任何人、或每個人都對得上,就跟以前一樣全員輪流。
|
||||
|
|
|
|||
52
src/httpd.rs
52
src/httpd.rs
|
|
@ -478,7 +478,15 @@ fn write_kept(c: &mut TcpStream, r: &Resp, keep: bool) -> bool {
|
|||
}
|
||||
true
|
||||
};
|
||||
if send(head.as_bytes()) && send(&r.body) {
|
||||
// (A small answer goes in one piece with its head: one packet, not two.)
|
||||
let sent = if r.body.len() <= 16 * 1024 {
|
||||
let mut whole = head.into_bytes();
|
||||
whole.extend_from_slice(&r.body);
|
||||
send(&whole)
|
||||
} else {
|
||||
send(head.as_bytes()) && send(&r.body)
|
||||
};
|
||||
if sent {
|
||||
c.flush().is_ok()
|
||||
} else {
|
||||
let _ = c.shutdown(std::net::Shutdown::Both);
|
||||
|
|
@ -581,6 +589,11 @@ fn serve_with(listener: TcpListener, handler: Handler, upgrade: Option<Upgrade>,
|
|||
match listener.accept() {
|
||||
Ok((mut c, peer)) => {
|
||||
c.set_nonblocking(false).ok();
|
||||
// Every answer goes out as soon as it is written: with Nagle on, the part after
|
||||
// the head waited for the client to acknowledge the head, which it holds back up
|
||||
// to 40 ms — every answer on a kept connection took 40–50 ms (the account server's
|
||||
// /api/hello, measured at home), and a phone's request to a brain pays it twice.
|
||||
c.set_nodelay(true).ok();
|
||||
if active.load(Ordering::SeqCst) >= MAX_CONNS {
|
||||
write_response(&mut c, &Resp::error(503, "伺服器忙碌中,請稍後再試"));
|
||||
continue;
|
||||
|
|
@ -954,6 +967,43 @@ pub fn open(key: &[u8; 32], sealed: &[u8]) -> Option<Vec<u8>> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Answers on a kept connection come back at once: the account server's /api/hello took 40–50
|
||||
/// ms each on Linux (the head and the body in two writes with Nagle on, against the client's
|
||||
/// delayed acknowledgement), and a phone's request through it to a brain paid that twice.
|
||||
#[test]
|
||||
fn answers_on_a_kept_connection_come_back_at_once() {
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
let h: Handler = Arc::new(|r: Req| Resp::json(&serde_json::json!({"path": r.path})));
|
||||
let l = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = l.local_addr().unwrap().port();
|
||||
std::thread::spawn(move || serve_kept(l, h, None, Arc::new(|| true)));
|
||||
let mut c = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
let mut rd = BufReader::new(c.try_clone().unwrap());
|
||||
let mut ask = || {
|
||||
c.write_all(b"GET /api/hello HTTP/1.1\r\nHost: x\r\n\r\n").unwrap();
|
||||
let mut n = 0usize;
|
||||
loop {
|
||||
let mut l = String::new();
|
||||
rd.read_line(&mut l).unwrap();
|
||||
if let Some(v) = l.strip_prefix("Content-Length: ") {
|
||||
n = v.trim().parse().unwrap();
|
||||
}
|
||||
if l == "\r\n" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut body = vec![0u8; n];
|
||||
rd.read_exact(&mut body).unwrap();
|
||||
};
|
||||
ask();
|
||||
let t = std::time::Instant::now();
|
||||
for _ in 0..10 {
|
||||
ask();
|
||||
}
|
||||
let each = t.elapsed() / 10;
|
||||
assert!(each < Duration::from_millis(15), "{each:?} an answer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn totp_matches_the_rfc_6238_vectors() {
|
||||
// RFC 6238's SHA-1 test secret, 8-digit codes; the last six of each.
|
||||
|
|
|
|||
|
|
@ -332,6 +332,8 @@ impl Drop for Carried {
|
|||
|
||||
fn reach(inner: SocketAddr, peer: IpAddr) -> Option<(TcpStream, Carried)> {
|
||||
let up = TcpStream::connect_timeout(&inner, Duration::from_secs(5)).ok()?;
|
||||
// (Both legs send what they have at once: see httpd's accept.)
|
||||
let _ = up.set_nodelay(true);
|
||||
let port = up.local_addr().ok()?.port();
|
||||
// (Before a byte goes through: the server reads who asked once it has the request.)
|
||||
crate::httpd::carried().lock().unwrap().insert(port, peer);
|
||||
|
|
@ -347,6 +349,7 @@ pub fn front(listener: TcpListener, inner: SocketAddr, cfg: Arc<rustls::ServerCo
|
|||
match listener.accept() {
|
||||
Ok((c, peer)) => {
|
||||
c.set_nonblocking(false).ok();
|
||||
c.set_nodelay(true).ok();
|
||||
if active.fetch_add(1, Ordering::SeqCst) >= MAX_CARRIED {
|
||||
active.fetch_sub(1, Ordering::SeqCst);
|
||||
continue;
|
||||
|
|
|
|||
Loading…
Reference in New Issue