Architecture
Plumeria is an HTTP/1.1 server whose connection code reads like blocking code ((let [b (read! c)] (write! c b) (close! c))) while never holding an OS thread per connection. Each connection runs on a Jolt fiber, and the sockets underneath are non-blocking: a call that would block parks the fiber, and Jolt resumes it when the socket is ready.
The layers
flowchart TD
socket["plumeria.socket<br/>listen! / accept! / read! / write! / close! over jolt.socket"]
conn["plumeria.conn<br/>buffered reader: read-head! / read-body! / read-chunked! / body-stream"]
parse["plumeria.parse<br/>analyze: the request head in one pass"]
proxy["plumeria.proxy<br/>:trusted-proxies, Forwarded / X-Forwarded-*"]
response["plumeria.response<br/>response renderer, body-kind"]
write["plumeria.write<br/>response writers, files on the disk pool"]
connection["plumeria.connection<br/>a fiber per connection: keep-alive loop, windows, meter"]
server["plumeria.server<br/>bind, privilege drop, accept loop, reaper, drain"]
core["plumeria.core<br/>run-server / http-listen: the public API"]
tls["plumeria.tls<br/>OpenSSL 3 over memory BIOs (only with :tls)"]
websocket["plumeria.websocket<br/>RFC 6455 (loaded on the first upgrade)"]
mw["middleware: router, gzip, static, files,<br/>https, multipart, log, json"]
ens["plumeria.ensemble<br/>run-server in a gen_server, worker pools<br/>(opt-in: needs jlt-commons/ensemble)"]
socket --> conn --> connection
parse --> proxy --> connection
response --> write --> connection
connection --> server --> core
socket --> tls -.-> connection
conn --> websocket -.-> connection
mw -.->|wraps the handler| core
core -.->|run-server| ens
Only socket touches java.net. conn turns the stream of reads into one complete request at a time, keeping leftover bytes for the next one, and parse reads the head. connection runs each connection's loop over those, write puts responses on the wire, and server owns the listener and the registry of open connections. core is the public API on top. The middleware namespaces sit outside the server: each wraps the handler and answers an ordinary response map, so the server writes and closes a compressed or static body exactly as it would any other.
Concurrency model
http-listen (and run-server, the Ring-adapter face of the same start, which answers a handle {:port :host :stop}) binds a ServerSocket, runs the accept loop on a fiber, and spawns one more fiber (jolt.fibers/spawn) for every accepted connection. It keeps a registry of open connections, which the connection cap (:max-connections, 503 past it), the write-deadline reaper and the stop fn's drain all read.
Jolt runs fibers on a pool of carrier OS threads, one per CPU core by default (jolt.fibers/carrier-count). A fiber stays on the carrier it started on, and the scheduler preempts it on a timer so a busy fiber can't starve the others on its carrier. Different carriers run in parallel, so two connections can be handled at the same instant on different cores.
What makes a socket call cheap is jolt.socket. Its sockets are non-blocking file descriptors; when accept, read or write would block, it registers the fd with jolt.io-poller (kqueue on macOS, epoll on Linux) and parks the fiber. A single poller thread watches every registered fd and resumes the fiber when its socket is ready. Other parking operations work the same way: Thread/sleep, channel ops, deref, locks.
Two consequences follow, and both are rules for anyone changing this code:
- A blocking foreign call on a connection fiber (raw
recvthroughjolt.ffi, a blocking C library) doesn't park. It pins the carrier and stalls every fiber queued on it. Handlers must stick to parking operations. - Because connections run in parallel, shared mutable state must be thread-safe: atoms, or
locking/ReentrantLock, which Jolt keeps correct across a park.
Request lifecycle (keep-alive)
plumeria.connection/serve-connection runs a per-connection loop, not a single request: plumeria.conn/read-head! (a complete request head, up to the terminating CRLFCRLF) → plumeria.parse/analyze, one pass over the head that answers {:request :framing :connection}: the Ring keys :request-method, :uri, :query-string, :protocol and :headers, the Connection tokens, and the body framing. (parse-head, frame-body and connection-tokens are the same three answers one at a time, kept for callers and tests; a test pins that they agree with analyze.) The framing is the RFC-7230 §3.3.3 body-framing decision (a non-tchar header field-name (or empty) → 400, a bare CR/LF anywhere in the head → 400, a Transfer-Encoding other than a lone chunked → 400 (or 501 for another coding before chunked), invalid/duplicate Content-Length → 400, else a byte length or chunked; header values are trimmed of SP/HTAB only, and a no-OWS Content-Length:5 parses correctly) → (if a body) conn/read-body!, or conn/read-chunked! for a chunked body (decoded whole, trailers checked and dropped; a malformed body is 400, oversized 413, oversized trailers 431) → connection adds the socket-derived keys (:scheme, :server-port, :server-name, :remote-addr; when the peer is in :trusted-proxies, plumeria.proxy/forwarded-request rewrites them from the Forwarded or X-Forwarded-* headers, taking the rightmost untrusted hop as the client) and wraps the bytes as :body, a ByteArrayInputStream → the handler → a dispatch on plumeria.response/body-kind → write → either read the next request (keep-alive) or close. The body kind picks the writer: a File goes out with its length as Content-Length, an InputStream or a sequential collection is written as chunks (or close-delimited for HTTP/1.0), and everything else is rendered whole by render-bytes. A :stream fn in the response map takes the chunked path too. An InputStream body is closed in a finally, whichever way the response ended. Framing, turning the stream of socket/read! chunks into "one complete request, keep the leftover bytes for the next one", lives entirely in conn.
Each connection reads through one 64 KB buffer of its own. A read asks for 4 KB, and only when that fills does it ask the socket what else has arrived (available()); it answers a fresh array of exactly what it read. jolt's InputStream.read allocates the length it is asked for on every call (jolt-lang/jolt#1500), so asking for the whole 64 KB cost 64 KB of garbage per request.
Keep-alive follows HTTP/1.1 rules: persist unless the request sends Connection: close; an HTTP/1.0 request must send Connection: keep-alive to persist. A streaming (:stream) response to an HTTP/1.0 request always closes, since HTTP/1.0 has no chunked coding: the bytes go out unframed and the close marks the end of the body. A connection is also capped at :max-requests (default 1000), and the response that reaches the cap closes it. Request-size limits (:max-head / :max-body) bound the request itself: a head with no terminator inside :max-head, or a body past :max-body, gets a 431/413 and the loop ends with Connection: close. An HTTP/1.1 request with Expect: 100-continue gets an interim 100 Continue just before its body is read (a chunked body always gets it, since its size isn't known up front), so the client sends it at once instead of waiting (curl waits 1 s); a body past :max-body gets its 413 with no 100 first.
Streamed request bodies
A request with a body that the :stream-body option picks (true, or a predicate on the request map without :body) skips the whole-body read. Its Content-Length is checked against :max-stream-body (over it is 413, no 100), the window switches to per-read mode (below), and the handler gets as :body the InputStream from conn/body-stream, which pulls decoded bytes from the reader as the handler reads. A chunked body goes through the same step-wise decoder as read-chunked!, so the chunked-body limits apply with :max-stream-body in place of :max-body. The 100 Continue is sent on the handler's first read rather than before the handler runs.
The stream's state atom records whether the body was read to its end and the first failed read's cause. A failed read throws IOException; when the handler lets it escape, core answers the cause's status (408, 400, 413, 431; a closed peer just closes) and closes the connection. A predicate that throws answers 500. When the handler returns, the stream is finished, so a stored stream can never read the next request's bytes. Before the response is written, the connection's fate is settled: a body read to its end keeps the normal keep-alive rules; a failed read, or a 100 the client is still waiting for, closes; otherwise conn/drain! discards up to 64 KB of what's left and the connection is kept only if that reaches the end. The drain runs before the response because it decides the Connection header.
Two clocks: an idle window and a request deadline
Each connection holds a small window, and every read honours it. arm-window! sets it before each request: until the request's first bytes arrive, a read may wait only until now + :idle-ms. Running out closes the connection without a response, since no request was made.
The first bytes of the request start a second clock. From then on, every read for the rest of the head and the whole body may wait only until first-byte + :request-ms. Running out answers 408 Request Timeout and closes. Bytes of a pipelined next request already in the buffer start that clock at once. conn/from-client turns the time left into each read's socket timeout, and answers :timeout (rather than :eof, which means the peer closed) when a window runs out.
A streamed body (above) uses a third mode instead: the window holds {:read-ms (:body-read-ms opts)}, and each read may wait :body-read-ms from its own start. There is no total deadline for such a body, and the handler's own work between reads isn't counted.
These are deadlines rather than per-read timeouts on purpose. With a plain setSoTimeout on every read, a client sending one byte every few seconds would restart the clock each time and hold the connection forever. Igropyr uses the same two-clock split (a gap timeout before the request, a deadline from its first byte).
Write deadline
jolt.socket writes have no timeout, so a client that stops reading would park a write forever once the kernel buffers fill. Every write goes out in 64 KB slices, and each slice stamps a deadline of :write-ms in the connection's registry entry. One reaper fiber per server closes any socket whose slice has run past it, and closing a socket wakes the write parked on it, which then answers false. The deadline is per slice rather than per response so that a healthy but slow client fetching a large body isn't cut off by a clock on the whole response: :write-ms bounds progress, not length.
Stopping
The stop fn closes the listening socket, then sets the server's stopping flag, so every in-flight request is answered with Connection: close. It closes each connection that is idle, meaning its window is armed and no byte of a request has arrived, and keeps checking every 10 ms until the registry is empty or :drain-ms runs out. At that point it closes whatever is left.
Errors
Failures the client didn't cause go to the :on-error hook as event maps; logging.md lists them.
What went away with libuv
Until October 2026 the socket layer was libuv over jolt.ffi, driven by a jolt.fiber namespace from a branch build of Jolt. That design needed rules which no longer apply, because Jolt now does the parking itself:
without-preemptionaround everyuv_run, so a timer interrupt couldn't switch fibers inside a libuv callback's C frame.- A park/resume handshake with a one-shot wakeup permit, so a callback firing before the fiber parked wasn't lost.
- Manual memory discipline: every
foreign-callable, buffer, request and handle freed at a defined point, handles torn down withuv_close. - libuv's read cases (
nread> 0, == 0, < 0) handled explicitly. - An interruptible read plus a recurring idle timer, because closing a socket couldn't unblock a parked read, and a still-unexplained jolt-core/libuv lost wakeup needed retries.
The design notes and the investigation of that lost wakeup are kept outside this repository.
Testing model
Tests are plain clojure.test scripts run with jolt run, each ending with a run-and-exit tail. socket_test covers the socket layer directly: concurrent echo round trips on fibers, a read timing out to :eof, :eof on a closed peer, accept! after close, write! to a closed socket, reads through a reused buffer answering fresh arrays, and a small read allocating only for what arrived. The wire tests (keepalive, framing, stream, sse, lifecycle, reject and more) start a real http-listen and drive it with plumeria.test-client, a small java.net.Socket client that sends raw request bytes and reads raw response bytes, so a test sees exactly what went on the wire. The client deliberately uses jolt.socket too: a raw-FFI socket client in the same process as a jolt.socket server broke the exchange (2026-10-08, not yet root-caused). See contributing.md to run the suite.