Kest

Search

Search the documentation

HTTP

HTTP adapters

Netty or the JDK server, and how requests are dispatched.

An engine implements the HttpAdapter interface. Kest ships two:

PackageUse
http-server-nettyNetty 4.2: epoll, kqueue or NIO, pooled buffers, TLS, HTTP/2, body limit, idle timeout, graceful stop
http-server-jdkthe JDK's built-in server, one virtual thread per request, zero dependencies; for tests and small services

Both run the same pipeline and pass the same test suite. The Netty options:

NettyHttpAdapter(
    port = 8080, host = "0.0.0.0",
    maxBodyBytes = 10 shl 20,           // bigger bodies are refused with 413
    idleTimeoutSeconds = 60,            // idle keep-alive connections are closed
    sslContext = null,                  // TLS
    http2 = false,                      // HTTP/2 as well as HTTP/1.1
    dispatch = Dispatch.VIRTUAL_THREADS,
)
Loading diagram…
  • Default: a virtual thread per request. Safe for JDBC and remote calls, at about twice the CPU per request.
  • @NonBlocking on a route or controller: runs on the I/O thread, roughly half the CPU. Middleware on the route must also be declared non-blocking.

It is a promise, and Kest checks it

Marking a blocking handler @NonBlocking stalls the I/O threads: in the benchmark a 2 ms blocking handler dropped from 19,077 to 3,539 requests per second. So the Netty engine runs a watchdog. A @NonBlocking route that holds an I/O thread for more than blockingThresholdMillis (10 ms by default) is logged with the route and the stack the thread was stuck in, and runs on virtual threads from then on:

[kest] @NonBlocking route GET /nb/slow (WatchedController.slow) held an I/O thread for over 10 ms, so it is no longer run inline.
    Remove @NonBlocking from it, or stop it blocking. The I/O thread was here: ...

The request that tripped it finishes where it is (a thread cannot be taken off a blocking call), so one request pays and the rest do not. The check costs a nanoTime and two volatile writes per inline request. NettyHttpAdapter(blockingThresholdMillis = 0) turns it off. A CPU-heavy handler is demoted the same way, since it stalls the loop just as much.

Numbers and caveats are on the benchmarks page.

startHttp(NettyHttpAdapter(8080, http2 = true))                                                // cleartext: h2c upgrade or prior knowledge
startHttp(NettyHttpAdapter(8443, sslContext = http2TlsContext(certChain, privateKey), http2 = true))   // TLS: ALPN picks h2 or http/1.1

Each HTTP/2 stream is handled like an HTTP/1.1 request, so routes, guards, pipes and filters are unchanged, and HTTP/1.1 clients keep working on the same port. Many requests multiplex over one connection. Tested with the JDK client over cleartext and TLS. Not covered: WebSocket and Server-Sent Events over HTTP/2 (WebSocket stays on HTTP/1.1), server push, and the JDK engine (Netty only).

Read more