Limits, timeouts and stopping

Every option here goes in the map that run-server and http-listen take.

optiondefault
:host"127.0.0.1""0.0.0.0" serves other machines
:max-head64 KiBrequest line and headers; over it is 431
:max-body10 MiBa buffered request body; over it is 413
:idle-ms15000wait for a request's first byte; running out closes silently
:request-ms30000from the first byte to the end of head and body; running out is 408
:max-requests1000requests per kept-alive connection (nginx's keepalive_requests)
:write-ms30000each 64 KB slice of a write must finish within this
:max-connections1024past it a new connection gets 503 and is closed
:drain-ms10000how long the stop fn lets in-flight requests finish
:handler-msnil (off)a sync handler running longer than this is killed and the client gets 503

Two clocks per request

Waiting for a request's first byte is bounded by :idle-ms, and running out closes the connection without a response, since no request was made. From the first byte, the rest of the head and the whole body must arrive within :request-ms, or the client gets 408. These are deadlines, not per-read timeouts, so a client that drips one byte at a time can't hold a connection open. Bytes of a pipelined request already in the buffer start the second clock at once.

The response that reaches :max-requests carries Connection: close. Keep-alive otherwise follows HTTP/1.1: a connection persists unless the request sends Connection: close, and an HTTP/1.0 request must send Connection: keep-alive to persist. Connection is read as a token list, so close, TE closes.

Writes

A write to a client that stopped reading gives up after :write-ms without progress and closes the connection. The clock is per 64 KB slice, so a slow but healthy client fetching a large body isn't cut off: :write-ms bounds progress, not length.

Handler deadlines

A sync handler has no time limit by default, so one that never returns holds its connection, and its carrier or handler-pool slot, until the client gives up. :handler-ms n puts a limit on it. A handler that hasn't returned its response n ms after it started running is killed, the client gets 503 with the body handler timed out and Connection: close, and the connection closes.

(http/run-server app {:port 18090 :handler-ms 5000})

The value is nil or a positive integer, and anything else throws ex-info before anything binds. So does :handler-ms together with :async? true, because an async handler can hand respond to any fiber and killing the one that called it stops nothing. Async handlers keep :async-timeout-ms.

What the clock covers:

  • The handler from its start to its return or throw, middleware included.
  • Reading a :stream-body request body, since the handler does that while it runs. For large uploads, set :handler-ms to cover the longest one, or leave it off.
  • Not the time a request waits for a free slot on a busy :handler-pool. The clock starts when the handler begins running, so a saturated pool doesn't turn healthy requests into 503s.
  • Not the response body. A :stream fn, an InputStream or a seq is produced after the handler returns and stays bounded by :write-ms per slice.

The kill can't be caught. A catch clause, even catch Throwable, never sees it, so a handler can't swallow it and carry on, but its finally blocks and with-open forms run on the way out. A handler parked in a sleep, a channel, a socket read or a deref dies at once, and a busy loop dies within a preemption quantum. On a handler pool the slot is free again right away.

A handler inside a blocking C call is the exception. A call bound with {:blocking true} isn't interrupted: a 1.5 s call runs to its end, and the client still gets the 503 at the deadline. The kill lands once the call returns, at the handler's next park or preemption, and its finally blocks run then. If the handler returns before reaching such a point, its result is thrown away, and a response whose body needs closing, such as an InputStream, has the body closed. Put handlers that call into C on a handler pool, so a stuck call holds only a pool carrier. Without a pool the handler runs on the default carriers, and time it waits there behind a carrier pinned by a blocking C call isn't counted either, which the same advice covers.

A killed request is reported to :on-error as :handler-timeout (see logging.md), and a streamed request body is not drained, since the connection closes. Stopping the server is unchanged: the drain closes sockets, and a handler still running is killed only by its own deadline.

With :handler-ms on, each request's handler runs on a fiber of its own (on the handler pool when :handler-pool picks the request) and the connection fiber joins it with the timeout. That costs one spawn and one join per request, measured in performance.md. With it off there is a nil check and a map lookup per response, and nothing else.

Request framing

Framing is strict, against request smuggling (RFC 9112):

  • The request line must be exactly METHOD SP target SP HTTP/d.d with a token method, or it's 400. A well-formed but unsupported version such as HTTP/9.9 gets 505.
  • A header line with no colon (an obs-fold continuation included), a field name that isn't a token or is empty, or whitespace before the colon is 400.
  • A bare CR or LF anywhere in the head is 400, and so is a head ended by bare LFs with no CRLFCRLF.
  • An invalid or duplicate Content-Length is 400. Transfer-Encoding rules are in requests-and-responses.md.
  • Header values are trimmed of spaces and tabs only.

The connection closes after any of these rejections.

Stopping

(http/stop-server handle), or the fn http-listen returns, drains: it closes the listening socket and every idle keep-alive connection, lets in-flight requests finish with Connection: close for up to :drain-ms (or pass a number to the stop fn), then closes the rest. It answers true when everything finished in time.

When a process runs out of file descriptors, the accept loop backs off and keeps accepting once fds are free again. :on-error sees :accept-failed.