Procházet zdrojové kódy

fix reitit db-test (#4795)

Tommi Reiman před 6 roky
rodič
revize
1a2415c239

+ 2 - 2
frameworks/Clojure/reitit/project.clj

@@ -1,12 +1,12 @@
 (defproject hello "reitit"
   :description "Immutant-nio, Reitit, Jsonista & Porsas"
   :dependencies [[org.clojure/clojure "1.10.0"]
-                 [ikitommi/immutant-web "3.0.0-alpha1"]
+                 [ikitommi/immutant-web "3.0.0-alpha2"]
                  [metosin/jsonista "0.2.2"]
                  [hikari-cp "2.7.1"]
                  [org.postgresql/postgresql "42.2.5"]
                  [metosin/porsas "0.0.1-alpha4"]
-                 [metosin/reitit "0.3.1"]]
+                 [metosin/reitit "0.3.3"]]
   :jvm-opts ^:replace ["-Dclojure.compiler.direct-linking=true"]
   :main hello.handler
   :aot :all)

+ 6 - 25
frameworks/Clojure/reitit/src/hello/handler.clj

@@ -2,22 +2,10 @@
   (:require [immutant.web :as web]
             [hikari-cp.core :as hikari]
             [reitit.ring :as ring]
-            [immutant.web.internal.ring :as immutant]
             [porsas.core :as p]
             [jsonista.core :as j])
-  (:gen-class)
-  (:import (java.util.concurrent ThreadLocalRandom)
-           (io.undertow.server HttpServerExchange)))
-
-(defn blocking [handler]
-  (fn [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)))))
+  (:import (java.util.concurrent ThreadLocalRandom))
+  (:gen-class))
 
 (defn random []
   (unchecked-inc (.nextInt (ThreadLocalRandom/current) 10000)))
@@ -25,7 +13,7 @@
 (def query-one (:query-one (p/compile {:row (p/rs->compiled-record)})))
 
 (defn random-world [ds]
-  (let [con (p/get-connection ds)]
+  (with-open [con (p/get-connection ds)]
     (query-one con ["SELECT id, randomnumber from WORLD where id=?" (random)])))
 
 (defn plain-text-handler [_]
@@ -46,20 +34,16 @@
 
 (defn -main [& _]
   (let [ds (hikari/make-datasource
-             {:maximum-pool-size 256
-              :pool-name "db-pool"
-              :adapter "postgresql"
+             {:jdbc-url "jdbc:postgresql://tfb-database:5432/hello_world"
               :username "benchmarkdbuser"
               :password "benchmarkdbpass"
-              :database-name "hello_world"
-              :server-name "tfb-database"
-              :port-number 5432})]
+              :maximum-pool-size 256})]
     (web/run
       (ring/ring-handler
         (ring/router
           [["/plaintext" plain-text-handler]
            ["/json" json-handler]
-           ["/db" (blocking (db-handler ds))]])
+           ["/db" (web/dispatch (db-handler ds))]])
         (ring/create-default-handler)
         {:inject-match? false
          :inject-router? false})
@@ -69,6 +53,3 @@
        :io-threads (* 2 (.availableProcessors (Runtime/getRuntime)))
        :worker-threads (* 8 (.availableProcessors (Runtime/getRuntime)))
        :server {:always-set-keep-alive false}})))
-
-(comment
-  (-main))