Async handlers and handler pools

Async handlers

With :async? true, plumeria calls Ring's three-argument form, (handler request respond raise), which Jetty's and http-kit's adapters take too. respond takes the response map and raise an exception. Either may be called from any thread or fiber, before or after the handler returns:

(http/run-server
  (fn [req respond raise]
    (future
      (try (respond {:status 200 :body (slow-lookup (:uri req))})
           (catch Exception e (raise e)))))
  {:port 18090 :async? true})

The connection waits, parked, for the first answer. A raise is answered 500 and reported as :handler-threw, like a sync handler's throw. If neither comes within :async-timeout-ms (default 30000), the client gets 503, the connection closes, and :on-error sees :async-timeout. Only the first answer counts: a later respond has its body closed and is dropped, and a later raise (or a throw after respond) is reported. Everything after the answer works as in sync mode: keep-alive, streamed bodies, files, WebSocket listeners, and :stream-body request bodies, which stay readable until the answer.

Every handler in the stack must take three arguments in this mode, since Ring has no way to tell the forms apart. plumeria's router, wrap-gzip, wrap-static, wrap-file-conditions, wrap-hsts, redirect-handler and wrap-multipart-params take both, and so does most of ring-core's middleware. A one-argument route handler under the async router fails with an arity error, answered 500.

Handler deadlines

A sync handler that never returns can be killed after :handler-ms. It doesn't apply to async handlers, and it works the same inline and on a handler pool. See limits-and-timeouts.md.

Handler pools

A handler that calls into C and waits there (a database driver, an image library, sleep(3) through FFI) holds the OS thread it runs on. Connections are fibers spread over a few carrier threads, and a fiber stays on its carrier for life, so every connection queued behind that carrier waits too. :handler-pool moves picked handlers off the connection fibers onto a jolt.fibers pool of their own:

;; every handler on a pool of 8 carriers, made and shut down by the server
(http/run-server app {:port 18090 :handler-pool {:size 8}})

;; only the requests the predicate picks
(http/run-server app {:port 18090
                      :handler-pool {:size 8 :when #(str/starts-with? (:uri %) "/report")}})

;; a pool the app made and owns; the server never shuts it down
(def reports (fib/pool {:name :reports :size 4}))
(http/run-server app {:port 18090 :handler-pool reports})
(http/run-server app {:port 18090 :handler-pool {:pool reports :when report?}})

The value is a pool, or a map with exactly one of :pool or :size (a positive integer) and an optional :when. :when gets the request the handler will get and answers truthy to run it on the pool. A var works, and a throw from it is answered 500 like a handler's. Leaving it out, or true, picks every request. A bad value throws ex-info before anything binds. A :size pool is named :plumeria-handler and shut down by the stop fn after the drain, while a :pool you pass outlives the server. run-server's handle has the pool as :handler-pool (nil when off).

For a picked request the handler runs on the pool and the connection fiber parks until it answers. The response body is produced there too: a :stream fn, an InputStream body and a lazy seq run on the pool and are written by the connection fiber. A :stream-body request body is read from the pool, so its 100 Continue goes out from there. Async handlers are called on the pool, and the connection waits for respond as before. Throws are answered as they are without a pool (500 and :handler-threw; an Error closes the connection), dynamic bindings reach the handler, and a pool that was shut down answers 500. File bodies already go to the disk pool (:file-threads) either way.

Size it like a thread pool, with about as many carriers as calls you expect to be blocked at once. A pool carrier stuck in a call still holds every fiber queued on it, so a pool that's too small queues picked requests behind each other, though the unpicked ones keep going. Two things the pool can't fix:

  • Bind blocking C calls with {:blocking true}. Without it ((ffi/defcfn c-sleep "usleep" [:uint32] :int)), Chez's collector waits for the call to return before it can run, so any thread that allocates stalls until then, pool or not. On jolt 0.8.25 an allocating fiber waited 1.44 s behind a 1.5 s usleep on a pool carrier, and 40 ms once the binding had {:blocking true}.
  • Stop can't interrupt a stuck call. The drain waits up to its timeout for a handler to answer, then the stop fn answers false, as for any connection that won't finish.

Running every request on the pool costs about 8 µs each (keep-alive /plaintext at 0.81 of the no-pool rate with 10 clients, 0.94 with 100, on jolt 0.8.25). With the option off there's no measurable cost. benchmark/README.md has the runs.