We ended up implementing hard timeout using CAS; no noticeable drop in throughput or p99 latency.
I thought I'd share in case others on the list may need it. Here is the code snippet (using Kotlin).
override fun handleRequest(exchange: HttpServerExchange) {
val timeoutWhenTrue = AtomicBoolean(true)
exchange.getIoThread().executeAfter({
if (timeoutWhenTrue.compareAndSet(true, false)) {
// we're first, so end the exchange
exchange.statusCode = StatusCodes.REQUEST_TIME_OUT
exchange.endExchange()
}
}, requestTimeoutMillis, TimeUnit.MILLISECONDS)
exchange.dispatch(SameThreadExecutor.INSTANCE, Runnable { launch {
//
// request servicing logic
//
if (timeoutWhenTrue.compareAndSet(true, false)) {
response.headers.forEach { exchange.responseHeaders.add(it.key, it.value }
exchange.statusCode = response.statusCode
response.response?.let { exchange.responseSender.send(response.response) } ?: exchange.endExchange() }
}})
}