| @@ -126,3 +126,42 @@ This ADR left three follow-ups: writes were not timed, so a client that stops re |
| 126 | 126 | - Unit scenarios in `louise_rt/src/builder.rs`: appends in place, growth, copies of shared builders, `build` in place. |
| 127 | 127 | - The load test: 64 keep-alive connections × 500 requests to the demo, release builds. 30,800 requests/s on one scheduler thread and 121,000 on eight (a loaded 22-core machine), every response checked, the leak oracle at 0 after each run. |
| 128 | 128 | |
| 129 | + |
| 130 | +## Revision 2 — 2026-10-15: slow and greedy clients, and name resolution |
| 131 | +**Status:** Proposed; needs engineering review (#44). Issues #220 and #222. |
| 132 | + |
| 133 | +### Context |
| 134 | +A review of `std/http` and `std/net` (issues #220, #222) found ways a few clients can hold a server, or a scheduler thread, with very little traffic: |
| 135 | + |
| 136 | +- **The idle timeout restarts on every read.** The connection actor sends `Reading` before each read, and the watchdog restarts `idle_timeout` on each one, so a client that sends one byte every few seconds keeps its connection, and its actor, forever ("slowloris"). |
| 137 | +- **Nothing limits open connections.** `connections` counts accepts in all, and is unlimited by default; each connection is an actor and a socket. |
| 138 | +- **A request head is copied and parsed again on every read.** `receive_more` concatenates the unread buffer with what arrives, and `parse_head` starts over from its beginning: quadratic in the head's size, up to the 64 KiB limit — which is also checked only on a partial head, before the read that may add 64 KiB more. |
| 139 | +- **A failing `accept` loops at once.** Out of descriptors (EMFILE), `serve_with` prints and accepts again, spinning a scheduler thread. |
| 140 | +- **Repeated `Transfer-Encoding` headers are not refused.** The first one is read; a proxy that reads the last one frames the request differently (request smuggling, RFC 9112 §6.1 and §6.3). |
| 141 | +- **Name resolution runs on a scheduler thread.** `net.connect` and `net.listen` resolve their host (`getaddrinfo`) inside the extern, which is not `blocking`: a slow resolver stalls the thread and every actor queued on it. And the port is truncated to 16 bits (`as u16`): 65616 is port 80. |
| 142 | + |
| 143 | +### Decision |
| 144 | +**A deadline per request head.** `Settings` gains `head_timeout` (10 s by default). The deadline starts when a head's first byte arrives, and the whole head must arrive before it: the watchdog is told when it starts (`Activity` gains a variant carrying it) and waits until then, not `idle_timeout` from the last read. `idle_timeout` keeps timing the wait for a request to start (keep-alive), and the reads of a body. The watchdog needs `clock` for the deadline (`time.monotonic`). |
| 145 | + |
| 146 | +**A limit on open connections.** `Settings` gains `max_open` (512 by default: under the usual descriptor limit of 1024, so the limit is reached before `accept` fails). `serve_with` counts the connections it has started and that have not ended: the watchdog, which already learns when its connection's actor stops, tells the acceptor — on that `Stopped` and also when it closes the connection itself on a timeout. At the limit, `serve_with` waits for one to end before accepting again; below it, it reads the notices without waiting. `connections` keeps its meaning: how many to accept in all. |
| 147 | + |
| 148 | +**A head is scanned once.** Arriving bytes go into a `Builder` (appended in place, Revision 1). A new extern, `tl_http_parse_head_end(bytes, from) -> Int`, finds the blank line that ends a head, from an offset, or -1; each read scans only what it brought, plus three bytes before it. When the end is found, the head is built once and `parse_head` runs once. The limit is checked against the head's size, so 64 KiB means 64 KiB; a request that arrives whole in one read is checked too. Bodies and chunks keep their current path. |
| 149 | + |
| 150 | +**`accept` backs off.** After a failed `accept`, `serve_with` sleeps, from 10 ms doubling to 1 s while it keeps failing, and starts again from 10 ms after a success. |
| 151 | + |
| 152 | +**One `Transfer-Encoding`.** `parse_head` refuses a request with more than one `Transfer-Encoding` header, as it refuses differing `Content-Length` values: `Invalid`, answered with 400 and a closed connection. |
| 153 | + |
| 154 | +**Resolution off the scheduler.** A new `blocking` extern (ADR 0005), `tl_net_resolve(host, port) -> Result<List<String>, String>`, resolves on the blocking pool and returns the addresses as text. `connect` and `listen` become Louise functions: they check the port (0 to 65535, else an `Error`), resolve, then try each address with the existing externs, which resolve an IP literal without a lookup, and return the last error if none works. A literal still takes the round trip to the pool; `listen` and `connect` are rare next to reads and writes. |
| 155 | + |
| 156 | +### Consequences |
| 157 | +- **Breaking:** `Settings` has two more fields. Callers that start from `http.defaults()` with a record update, as every one in the repository does, are unaffected. |
| 158 | +- **A server under a burst of connections** waits at `max_open` instead of failing `accept`; clients queue in the kernel's listen backlog. |
| 159 | +- **A slow client** has `head_timeout` for its whole head, however it trickles it. |
| 160 | +- **Cost per request:** one more watchdog message when a head starts; no copy of the head per read. |
| 161 | +- **Runtime ABI:** two new externs (`tl_net_resolve`, `tl_http_parse_head_end`); no layout or calling-convention change, so `TL_ABI_VERSION` stays. |
| 162 | +- **Still not timed:** the handler, as before. |
| 163 | + |
| 164 | +### Verification |
| 165 | +- `crates/louise/tests/cli.rs`, with fixtures in `tests/fixtures/http`: a trickled head closed at `head_timeout`; a keep-alive connection idle longer than `head_timeout` (but under `idle_timeout`) still served; `max_open` + 1 connections, the last one served only after another ends; a connection closed by its watchdog frees its place; a repeated `Transfer-Encoding` answered 400; a head of 64 KiB + 1 that arrives in two reads answered 431. The leak oracle reads 0 at exit. |
| 166 | +- Unit tests of `parse_head_end` and of the repeated `Transfer-Encoding` in `louise_rt/src/http.rs`; of `resolve` and of out-of-range ports in `louise_rt/src/net.rs` and a fixture. |
| 167 | +- The demo under load (`the_demo_under_load`), with the leak oracle at 0. |