Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(server): keep TCP_NODELAY on gRPC when adopting a pre-bound listener

xerj-org51cd68892537
diff --git a/engine/crates/xerj-server/Cargo.toml b/engine/crates/xerj-server/Cargo.toml
index 32ba663a5..3f8f0d786 100644
--- a/engine/crates/xerj-server/Cargo.toml
+++ b/engine/crates/xerj-server/Cargo.toml
@@ -52,10 +52,6 @@ rayon = { workspace = true }
# gRPC server: the real XerjSearch service on :8081 (proto: ../../proto/xerj.proto).
tonic.workspace = true
-# `TcpListenerStream`, so the gRPC listener can be bound up front alongside
-# the REST ones and a taken port fails startup rather than being logged
-# under a banner that already claimed it (issue #465).
-tokio-stream = { version = "0.1", features = ["net"] }
prost.workspace = true
# Use jemalloc as the global allocator. The default glibc malloc causes
@@ -102,3 +98,4 @@ protox = { workspace = true }
reqwest = { workspace = true }
tempfile = { workspace = true }
flate2 = "1"
+tokio-stream = "0.1"
diff --git a/engine/crates/xerj-server/src/grpc.rs b/engine/crates/xerj-server/src/grpc.rs
index 5f7f08b3d..7cf6f0a78 100644
--- a/engine/crates/xerj-server/src/grpc.rs
+++ b/engine/crates/xerj-server/src/grpc.rs
@@ -436,8 +436,24 @@ where
let addr = listener
.local_addr()
.context("gRPC: read the bound address")?;
+ // `TcpIncoming::from_listener`, NOT a bare `TcpListenerStream`.
+ //
+ // `serve_with_incoming_shutdown` "discards any provided Server TCP
+ // configuration" — including `Server::builder()`'s `tcp_nodelay: true`,
+ // which tonic applies only inside `TcpIncoming::poll_next`. Handing it a
+ // raw stream left Nagle on for every accepted gRPC connection and put
+ // Linux's 40 ms delayed-ACK back on unary RPCs: measured p50 0.31 ms ->
+ // 40.9 ms on loopback, against an unaffected ES-compat control on the same
+ // node. `main.rs` sets the same option on the REST path for the same
+ // reason ("a fixed per-request latency tax on trivial reads").
+ //
+ // `from_listener` keeps the bind-up-front property of #465 — the listener
+ // is already bound, this only adopts it — while restoring the per-accept
+ // socket options.
let listener =
tokio::net::TcpListener::from_std(listener).context("gRPC: adopt the bound listener")?;
+ let incoming = tonic::transport::server::TcpIncoming::from_listener(listener, true, None)
+ .map_err(|e| anyhow::anyhow!("gRPC: drive the bound listener: {e}"))?;
let interceptor = GrpcAuth {
state: state.clone(),
};
@@ -445,10 +461,7 @@ where
info!("gRPC XerjSearch listening on {addr}");
tonic::transport::Server::builder()
.add_service(svc)
- .serve_with_incoming_shutdown(
- tokio_stream::wrappers::TcpListenerStream::new(listener),
- shutdown,
- )
+ .serve_with_incoming_shutdown(incoming, shutdown)
.await
.context("gRPC transport error")?;
Ok(())
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rs
index aa83b3a44..a6abd9a87 100644
--- a/engine/crates/xerj-server/src/main.rs
+++ b/engine/crates/xerj-server/src/main.rs
@@ -360,7 +360,7 @@ fn url_host(bind: &str) -> String {
///
/// The banner is printed from *these*, never from `cfg.server.*_port`, because
/// a port in the config is a request and a port in here is a fact. See
-/// `bind_listeners` for why that distinction is load-bearing (issue #465).
+/// step 10a of `async_main` for why that distinction is load-bearing (#465).
struct BoundAddrs {
rest: SocketAddr,
es: SocketAddr,
@@ -1017,9 +1017,10 @@ fn spawn_periodic_flusher(
/// Take an address and bind it, then serve. Kept for tests, which want the
/// convenience of `:0` and do not care about startup ordering.
///
-/// The server itself must NOT use this: it binds every listener up front, in
-/// `bind_listeners`, so that a taken port is a startup failure rather than a
-/// line in the log under a banner that already claimed success (issue #465).
+/// The server itself must NOT use this: it binds every listener up front at
+/// step 10a of `async_main`, so that a taken port is a startup failure rather
+/// than a line in the log under a banner that already claimed success
+/// (issue #465).
#[cfg(test)]
async fn serve(
router: Router,
diff --git a/engine/crates/xerj-server/tests/taken_port_fails_startup.rs b/engine/crates/xerj-server/tests/taken_port_fails_startup.rs
index f4eebf3e0..e3ee1e1f5 100644
--- a/engine/crates/xerj-server/tests/taken_port_fails_startup.rs
+++ b/engine/crates/xerj-server/tests/taken_port_fails_startup.rs
@@ -198,12 +198,23 @@ fn a_taken_es_compat_port_ends_the_process() {
reading the exit code learns nothing.\n--- stderr ---\n{}",
out.stderr
);
+ // Deliberately matched against the bind error and not the bare port number:
+ // the first-launch console link (`http://127.0.0.1:{es}/_xerj-console/setup`)
+ // also reaches stderr, and before the fix, so `stderr.contains(port)` was
+ // satisfied whether or not the failure was ever reported. Verified by
+ // dropping `{addr}` from `bind_listener`'s context and watching the weaker
+ // form still pass.
assert!(
- out.stderr.contains(&es.to_string()),
+ out.stderr.contains(&format!("bind 127.0.0.1:{es}")),
"the failure must name the port that was taken, so the operator can \
act on it without reading the log\n--- stderr ---\n{}",
out.stderr
);
+ assert!(
+ out.stderr.contains("Address already in use"),
+ "the failure must say why the port could not be taken\n--- stderr ---\n{}",
+ out.stderr
+ );
drop(squatter);
}