diff --git a/crates/control-protocol/src/lib.rs b/crates/control-protocol/src/lib.rs index 8bdb08d660..d62f1488fa 100644 --- a/crates/control-protocol/src/lib.rs +++ b/crates/control-protocol/src/lib.rs @@ -1,7 +1,7 @@ //! Authenticated JSON-lines request framing and dispatch for local control services. #![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] use serde_json::{Value, json}; -use std::io::{BufRead, BufReader, Read, Write}; +use std::io::{BufRead, BufReader, BufWriter, Read, Write}; use subtle::ConstantTimeEq; /// Whether a session token has the control protocol's 64 hexadecimal character form. @@ -120,18 +120,30 @@ pub fn serve_lines(read: R, mut out: W, token: &str, max_line if let Some(o) = r.as_object_mut() { o.insert("id".into(), id); } - if writeln!(out, "{r}").and_then(|()| out.flush()).is_err() { + if write_line(&mut out, &r).is_err() { return; } continue; } }, }; - let _ = writeln!(out, "{}", json!({"ok": false, "error": reject})).and_then(|()| out.flush()); + let _ = write_line(&mut out, &json!({"ok": false, "error": reject})); return; } } +/// Write `reply` and its newline to `out` through a buffer, then flush. `writeln!` with a JSON +/// value writes each string, number and separator separately, which is one system call apiece on +/// an unbuffered socket. The buffer holds at most `REPLY_BUFFER` bytes, so a large reply is not +/// copied whole before it is sent. +fn write_line(out: &mut impl Write, reply: &Value) -> std::io::Result<()> { + const REPLY_BUFFER: usize = 64 * 1024; + let mut buffered = BufWriter::with_capacity(REPLY_BUFFER, out); + serde_json::to_writer(&mut buffered, reply).map_err(std::io::Error::other)?; + buffered.write_all(b"\n")?; + buffered.flush() +} + #[cfg(test)] mod tests { use super::*; @@ -193,6 +205,36 @@ mod tests { assert_eq!(calls, 1); } #[test] + fn replies_reach_the_writer_in_few_bounded_writes() { + struct Writes(Vec>); + impl Write for Writes { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.push(buf.to_vec()); + Ok(buf.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let input = b"{\"id\":1,\"method\":\"tree\",\"token\":\"s\"}\n{\"id\":2,\"method\":\"tree\"}\n"; + let mut writes = Writes(Vec::new()); + serve_lines(&input[..], &mut writes, "s", 1024, |_, _| Some(json!({"ok": true, "result": {"names": ["a", "b", "c"], "sizes": [1, 2, 3]}}))); + assert_eq!(writes.0.len(), 2, "one write for the reply and one for the rejection"); + for line in &writes.0 { + assert_eq!(line.last(), Some(&b'\n')); + serde_json::from_slice::(line).unwrap(); + } + // A large reply goes out in a few buffer-sized writes, never as one whole copy. + let items: Vec = (0..100_000).collect(); + let mut writes = Writes(Vec::new()); + serve_lines(&b"{\"method\":\"tree\",\"token\":\"s\"}\n"[..], &mut writes, "s", 1024, |_, _| Some(json!({"ok": true, "result": items}))); + let total: usize = writes.0.iter().map(Vec::len).sum(); + assert!(writes.0.len() < items.len() / 100); + assert!(writes.0.iter().all(|w| w.len() < total)); + let reply: Value = serde_json::from_slice(&writes.0.concat()).unwrap(); + assert_eq!(reply["result"].as_array().map(Vec::len), Some(items.len())); + } + #[test] fn client_request_and_reply_keep_optional_fields() { let line = request_line(7, None, "status", &json!({"a": 1})); assert_eq!(serde_json::from_str::(&line).unwrap(), json!({"id": 7, "token": null, "method": "status", "params": {"a": 1}}));