Browse Source

Update ring-http-exchange with better executor (#10224)

Co-authored-by: jj <[email protected]>
ruroru 1 month ago
parent
commit
9e8d965057

+ 3 - 3
frameworks/Clojure/ring-http-exchange/project.clj

@@ -10,12 +10,12 @@
                  [seancorfield/next.jdbc "1.2.659"]
                  [org.clojars.jj/majavat "1.12.3"]
                  [hikari-cp "3.3.0"]
+                 [org.clojars.jj/boa-sql "1.0.0"]
+                 [io.netty/netty-transport-native-epoll "4.2.7.Final" :classifier "linux-x86_64"]
                  [org.postgresql/postgresql "42.7.8"]
                  [metosin/jsonista "0.3.13"]
                  ]
 
   :profiles {:robaho {:dependencies [[io.github.robaho/httpserver "1.0.28"]]}}
   :resource-paths ["resources"]
-  :main ring-http-exchange.benchmark
-
-  )
+  :main ring-http-exchange.benchmark)

+ 1 - 0
frameworks/Clojure/ring-http-exchange/resources/fortune.sql

@@ -0,0 +1 @@
+SELECT * FROM "Fortune";

+ 38 - 31
frameworks/Clojure/ring-http-exchange/src/ring_http_exchange/benchmark.clj

@@ -1,27 +1,30 @@
 (ns ring-http-exchange.benchmark
   (:gen-class)
   (:require
-    [jsonista.core :as json]
     [jj.majavat :as majavat]
     [jj.majavat.renderer :refer [->StringRenderer]]
     [jj.majavat.renderer.sanitizer :refer [->Html]]
-    [ring-http-exchange.core :as server]
-    [next.jdbc :as jdbc]
-    [next.jdbc.connection :as connection])
+    [jj.sql.boa :as boa]
+    [jsonista.core :as json]
+    [next.jdbc.connection :as connection]
+    [ring-http-exchange.core :as server])
   (:import
     (com.zaxxer.hikari HikariDataSource)
-    (java.util.concurrent Executors)))
+    (io.netty.channel.epoll EpollEventLoopGroup)))
 
-(def db-spec
-  {:jdbcUrl "jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass"})
+(def query-fortunes (boa/execute (boa/->NextJdbcAdapter) "fortune.sql"))
 
-(def datasource
-  (connection/->pool HikariDataSource db-spec))
-
-(defn query-fortunes []
-  (jdbc/execute! datasource
-                 ["SELECT * FROM \"Fortune\""]
-                 {:builder-fn next.jdbc.result-set/as-unqualified-lower-maps}))
+(def db-spec {:auto-commit        true
+              :read-only          false
+              :connection-timeout 30000
+              :validation-timeout 5000
+              :idle-timeout       600000
+              :max-lifetime       1800000
+              :minimum-idle       10
+              :maximum-pool-size  520
+              :minimum-pool-size  512
+              :register-mbeans    false
+              :jdbcUrl            "jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass"})
 
 (def ^:private ^:const additional-message {:id      0
                                            :message "Additional fortune added at request time."})
@@ -37,23 +40,27 @@
                                                       {:renderer (->StringRenderer
                                                                    {:sanitizer (->Html)})}))
 
+(defn- get-body [datasource]
+  (let [context (as-> (query-fortunes datasource) fortunes
+                      (conj fortunes additional-message)
+                      (sort-by :message fortunes))]
+    (render-fortune {:messages context})))
+
 (defn -main
   [& _]
   (println "Starting server on port 8080")
-  (server/run-http-server
-    (fn [req]
-      (case (req :uri)
-        "/json" {:status  200
-                 :headers json-headers
-                 :body    (json/write-value-as-bytes {:message "Hello, World!"})}
-        "/fortunes" (let [input (as-> (query-fortunes) fortunes
-                                      (conj fortunes additional-message)
-                                      (sort-by :message fortunes))
-                          body (render-fortune {:messages input})]
-                      {:status  200
-                       :headers fortune-headers
-                       :body    body})
-        plaintext-response))
-    {:port     8080
-     :host     "0.0.0.0"
-     :executor (Executors/newVirtualThreadPerTaskExecutor)}))
+  (let [datasource (connection/->pool HikariDataSource db-spec)]
+    (server/run-http-server
+      (fn [req]
+        (case (req :uri)
+          "/json" {:status  200
+                   :headers json-headers
+                   :body    (json/write-value-as-bytes {:message "Hello, World!"})}
+          "/fortunes" (let [body (get-body datasource)]
+                        {:status  200
+                         :headers fortune-headers
+                         :body    body})
+          plaintext-response))
+      {:port     8080
+       :host     "0.0.0.0"
+       :executor (EpollEventLoopGroup.)})))