Skip to content

Commit 9e8d965

Browse files
rurorujj
andauthored
Update ring-http-exchange with better executor (#10224)
Co-authored-by: jj <[email protected]>
1 parent 8249c30 commit 9e8d965

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
[seancorfield/next.jdbc "1.2.659"]
1111
[org.clojars.jj/majavat "1.12.3"]
1212
[hikari-cp "3.3.0"]
13+
[org.clojars.jj/boa-sql "1.0.0"]
14+
[io.netty/netty-transport-native-epoll "4.2.7.Final" :classifier "linux-x86_64"]
1315
[org.postgresql/postgresql "42.7.8"]
1416
[metosin/jsonista "0.3.13"]
1517
]
1618

1719
:profiles {:robaho {:dependencies [[io.github.robaho/httpserver "1.0.28"]]}}
1820
:resource-paths ["resources"]
19-
:main ring-http-exchange.benchmark
20-
21-
)
21+
:main ring-http-exchange.benchmark)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT * FROM "Fortune";
Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
(ns ring-http-exchange.benchmark
22
(:gen-class)
33
(:require
4-
[jsonista.core :as json]
54
[jj.majavat :as majavat]
65
[jj.majavat.renderer :refer [->StringRenderer]]
76
[jj.majavat.renderer.sanitizer :refer [->Html]]
8-
[ring-http-exchange.core :as server]
9-
[next.jdbc :as jdbc]
10-
[next.jdbc.connection :as connection])
7+
[jj.sql.boa :as boa]
8+
[jsonista.core :as json]
9+
[next.jdbc.connection :as connection]
10+
[ring-http-exchange.core :as server])
1111
(:import
1212
(com.zaxxer.hikari HikariDataSource)
13-
(java.util.concurrent Executors)))
13+
(io.netty.channel.epoll EpollEventLoopGroup)))
1414

15-
(def db-spec
16-
{:jdbcUrl "jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass"})
15+
(def query-fortunes (boa/execute (boa/->NextJdbcAdapter) "fortune.sql"))
1716

18-
(def datasource
19-
(connection/->pool HikariDataSource db-spec))
20-
21-
(defn query-fortunes []
22-
(jdbc/execute! datasource
23-
["SELECT * FROM \"Fortune\""]
24-
{:builder-fn next.jdbc.result-set/as-unqualified-lower-maps}))
17+
(def db-spec {:auto-commit true
18+
:read-only false
19+
:connection-timeout 30000
20+
:validation-timeout 5000
21+
:idle-timeout 600000
22+
:max-lifetime 1800000
23+
:minimum-idle 10
24+
:maximum-pool-size 520
25+
:minimum-pool-size 512
26+
:register-mbeans false
27+
:jdbcUrl "jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass"})
2528

2629
(def ^:private ^:const additional-message {:id 0
2730
:message "Additional fortune added at request time."})
@@ -37,23 +40,27 @@
3740
{:renderer (->StringRenderer
3841
{:sanitizer (->Html)})}))
3942

43+
(defn- get-body [datasource]
44+
(let [context (as-> (query-fortunes datasource) fortunes
45+
(conj fortunes additional-message)
46+
(sort-by :message fortunes))]
47+
(render-fortune {:messages context})))
48+
4049
(defn -main
4150
[& _]
4251
(println "Starting server on port 8080")
43-
(server/run-http-server
44-
(fn [req]
45-
(case (req :uri)
46-
"/json" {:status 200
47-
:headers json-headers
48-
:body (json/write-value-as-bytes {:message "Hello, World!"})}
49-
"/fortunes" (let [input (as-> (query-fortunes) fortunes
50-
(conj fortunes additional-message)
51-
(sort-by :message fortunes))
52-
body (render-fortune {:messages input})]
53-
{:status 200
54-
:headers fortune-headers
55-
:body body})
56-
plaintext-response))
57-
{:port 8080
58-
:host "0.0.0.0"
59-
:executor (Executors/newVirtualThreadPerTaskExecutor)}))
52+
(let [datasource (connection/->pool HikariDataSource db-spec)]
53+
(server/run-http-server
54+
(fn [req]
55+
(case (req :uri)
56+
"/json" {:status 200
57+
:headers json-headers
58+
:body (json/write-value-as-bytes {:message "Hello, World!"})}
59+
"/fortunes" (let [body (get-body datasource)]
60+
{:status 200
61+
:headers fortune-headers
62+
:body body})
63+
plaintext-response))
64+
{:port 8080
65+
:host "0.0.0.0"
66+
:executor (EpollEventLoopGroup.)})))

0 commit comments

Comments
 (0)