at work, we use pedestal interceptors for data processing. we have a new service that consumes form kafka and sends out requests and spend like 99% of its time waiting. i was curious how hard it would be to make use of virtual threads and it ended up being super easy. putting this snippet first in chain increased throughput tremendously. using a semaphore to apply back pressure otherwise it consumes so many events from kafka simultaneously that it runs out of memory.
(def vthread
"Lifts interceptor execution into a VirtualThread."
(let [semaphore (Semaphore/new 1000 true)]
{:name ::vthread
:enter (fn [context]
(Semaphore/.acquire semaphore)
(Thread/startVirtualThread
(fn []
(try (chain/execute context)
(finally (Semaphore/.release semaphore)))))
(chain/terminate context))}))