From d7af6cdb684f49d9216e8bce0d40297316375df1 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Sat, 14 May 2022 17:57:35 -0400 Subject: [PATCH] Require virtual threads Update microhttp_ring_adapter.clj Update microhttp_ring_adapter.clj --- README.md | 3 ++- src/dev/mccue/microhttp_ring_adapter.clj | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8ccf2a1..e6dd907 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Microhttp Ring Adapter +# THIS REQUIRES VIRTUAL THREADS. JDK 19 + --enable-preview ## What Adapter for using [microhttp](https://github.com/ebarlas/microhttp) as a ring server. @@ -15,7 +16,7 @@ I am confident it is as production ready as microhttp. ## deps.edn ```clojure -io.github.bowbahdoe/microhttp-ring-adapter {:git/tag "v0.0.1"} +io.github.bowbahdoe/microhttp-ring-adapter {:git/tag "v0.0.2"} ``` ## Usage diff --git a/src/dev/mccue/microhttp_ring_adapter.clj b/src/dev/mccue/microhttp_ring_adapter.clj index e55d400..37d0a3a 100644 --- a/src/dev/mccue/microhttp_ring_adapter.clj +++ b/src/dev/mccue/microhttp_ring_adapter.clj @@ -76,20 +76,25 @@ out) (Response. (:status ring-response) (or (status->reason (:status ring-response)) "IDK") - (reduce (fn [header-list [k v]] - (conj header-list (Header. k v))) - [] - (:headers ring-response)) + (mapv #(Header. (key %) (val %)) + (:headers ring-response)) (.toByteArray out)))) (defn- ring-handler->Handler [{:keys [host port]} ring-handler] (reify Handler (handle [_ microhttp-request callback] - (let [ring-request (->req {:host host - :port port} microhttp-request) - microhttp-response (<-res (ring-handler ring-request))] - (.accept callback microhttp-response))))) + (let [ring-request (->req {:host host + :port port} microhttp-request)] + (Thread/startVirtualThread + (fn [] + (.accept callback + (<-res (try + (ring-handler ring-request) + (catch Exception e + {:status 500 + :headers {} + :body (.getMessage e)})))))))))) (defn create-microhttp-event-loop ([handler]