 
 (defn blocking [handler]
   (fn [req]
-    (.startBlocking ^HttpServerExchange (:server-exchange req))
-    (handler req)))
+    (let [exchange ^HttpServerExchange (:server-exchange req)]
+      (if (.isInIoThread exchange)
+        (.dispatch exchange ^Runnable ^:once (fn []
+                                               (.startBlocking exchange)
+                                               (immutant/write-response exchange (handler req))
+                                               (.endExchange exchange)))
+        (handler req)))))
 
 (defn random []
   (unchecked-inc (.nextInt (ThreadLocalRandom/current) 10000)))

  (:require [immutant.web :as web]
            [hikari-cp.core :as hikari]
            [reitit.ring :as ring]
            [porsas.core :as p]
            [jsonista.core :as j])
  (:gen-class)

           ["/json" json-handler]
           ["/db" (blocking (db-handler ds))]])
        (ring/create-default-handler)
        {:inject-match? false
         :inject-router? false})
      {:port 8080
       :host "0.0.0.0"
       :io-threads (* 2 (.availableProcessors (Runtime/getRuntime)))
       :dispatch? false
       :worker-threads 48
       :server {:always-set-keep-alive false}})))

(comment
