Skip to content

Commit a3a25ff

Browse files
authored
Merge pull request #534 from k13gomez/add-loom-and-connector-config
Add Jetty options for acceptor/selector threads
2 parents 8fcd3b6 + 7e63583 commit a3a25ff

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

ring-jetty-adapter/src/ring/adapter/jetty.clj

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,20 @@
177177
(.setAllowNullPathInfo true)
178178
(JettyWebSocketServletContainerInitializer/configure nil)))
179179

180-
(defn- server-connector ^ServerConnector [^Server server & factories]
181-
(ServerConnector. server #^"[Lorg.eclipse.jetty.server.ConnectionFactory;"
182-
(into-array ConnectionFactory factories)))
180+
(defn- server-connector ^ServerConnector [^Server server factories options]
181+
(let [acceptors (options :acceptor-threads -1)
182+
selectors (options :selector-threads -1)]
183+
(ServerConnector. server (int acceptors) (int selectors)
184+
^"[Lorg.eclipse.jetty.server.ConnectionFactory;"
185+
(into-array ConnectionFactory factories))))
183186

184187
(defn- unix-domain-server-connector ^UnixDomainServerConnector
185-
[^Server server & factories]
186-
(UnixDomainServerConnector. server #^"[Lorg.eclipse.jetty.server.ConnectionFactory;"
187-
(into-array ConnectionFactory factories)))
188+
[^Server server factories options]
189+
(let [acceptors (options :acceptor-threads -1)
190+
selectors (options :selector-threads -1)]
191+
(UnixDomainServerConnector. server (int acceptors) (int selectors)
192+
^"[Lorg.eclipse.jetty.server.ConnectionFactory;"
193+
(into-array ConnectionFactory factories))))
188194

189195
(defn- http-config ^HttpConfiguration [options]
190196
(doto (HttpConfiguration.)
@@ -196,7 +202,7 @@
196202

197203
(defn- http-connector ^ServerConnector [server options]
198204
(let [http-factory (HttpConnectionFactory. (http-config options))]
199-
(doto (server-connector server http-factory)
205+
(doto (server-connector server [http-factory] options)
200206
(.setPort (options :port 80))
201207
(.setHost (options :host))
202208
(.setIdleTimeout (options :max-idle-time 200000)))))
@@ -248,7 +254,7 @@
248254
(when-let [scan-interval (options :keystore-scan-interval)]
249255
(.addBean server (doto (KeyStoreScanner. ssl-context)
250256
(.setScanInterval scan-interval))))
251-
(doto (server-connector server ssl-factory http-factory)
257+
(doto (server-connector server [ssl-factory http-factory] options)
252258
(.setPort ssl-port)
253259
(.setHost (options :host))
254260
(.setIdleTimeout (options :max-idle-time 200000)))))
@@ -257,7 +263,7 @@
257263
(let [http-factory (HttpConnectionFactory. (http-config options))
258264
socket (io/file (options :unix-socket))]
259265
(.deleteOnExit socket)
260-
(doto (unix-domain-server-connector server http-factory)
266+
(doto (unix-domain-server-connector server [http-factory] options)
261267
(.setUnixDomainPath (.toPath socket))
262268
(.setIdleTimeout (options :max-idle-time 200000)))))
263269

@@ -328,6 +334,8 @@
328334
:thread-pool - custom thread pool instance for Jetty to use
329335
:truststore - a truststore to use for SSL connections
330336
:trust-password - the password to the truststore
337+
:acceptor-threads - the number of acceptor threads to use
338+
:selector-threads - the number of selector threads to use
331339
:max-threads - the maximum number of threads to use (default 50)
332340
:min-threads - the minimum number of threads to use (default 8)
333341
:max-queued-requests - the maximum number of requests to be queued

ring-jetty-adapter/test/ring/adapter/test/jetty.clj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@
126126
(.send))]
127127
(is (= (.getStatus response) 200))
128128
(is (.getMediaType response) "text/plain")
129-
(is (= (.getContentAsString response) "Hello World")))))))
129+
(is (= (.getContentAsString response) "Hello World"))))
130+
(testing "with custom connector options"
131+
(let [server (run-jetty hello-world {:http? false
132+
:unix-socket test-unix-domain-socket
133+
:join? false
134+
:acceptor-threads 2})]
135+
(is (= 2 (-> server (.getConnectors) first (.getAcceptors))))
136+
(.stop server))))))
130137

131138
(testing "HTTPS server"
132139
(with-server hello-world {:port test-port
@@ -329,6 +336,22 @@
329336
(is (= 1000 (. thread-pool getIdleTimeout)))
330337
(.stop server)))
331338

339+
(testing "using default connector options"
340+
(let [server (run-jetty hello-world {:port test-port
341+
:join? false})]
342+
(is (>= 1 (-> server (.getConnectors) first (.getAcceptors))))
343+
(is (> (-> server (.getConnectors) first (.getSelectorManager) (.getSelectorCount)) 1))
344+
(.stop server)))
345+
346+
(testing "using custom connector options"
347+
(let [server (run-jetty hello-world {:port test-port
348+
:join? false
349+
:acceptor-threads 2
350+
:selector-threads 8})]
351+
(is (= 2 (-> server (.getConnectors) first (.getAcceptors))))
352+
(is (= 8 (-> server (.getConnectors) first (.getSelectorManager) (.getSelectorCount))))
353+
(.stop server)))
354+
332355
(testing "providing custom thread-pool"
333356
(let [pool (QueuedThreadPool.)
334357
server (run-jetty hello-world {:port test-port

0 commit comments

Comments
 (0)