|
19 | 19 | [org.eclipse.jetty.unixdomain.server UnixDomainServerConnector]
|
20 | 20 | [org.eclipse.jetty.ee9.servlet ServletContextHandler ServletHandler]
|
21 | 21 | [org.eclipse.jetty.util BlockingArrayQueue]
|
22 |
| - [org.eclipse.jetty.util.thread ThreadPool QueuedThreadPool] |
| 22 | + [org.eclipse.jetty.util.thread ThreadPool QueuedThreadPool VirtualThreadPool] |
23 | 23 | [org.eclipse.jetty.util.ssl SslContextFactory$Server KeyStoreScanner]
|
24 | 24 | [org.eclipse.jetty.ee9.nested Request]
|
25 | 25 | [org.eclipse.jetty.ee9.websocket.server
|
|
168 | 168 | (.setAllowNullPathInfo true)
|
169 | 169 | (JettyWebSocketServletContainerInitializer/configure nil)))
|
170 | 170 |
|
171 |
| -(defn- server-connector ^ServerConnector [^Server server & factories] |
172 |
| - (ServerConnector. server #^"[Lorg.eclipse.jetty.server.ConnectionFactory;" |
173 |
| - (into-array ConnectionFactory factories))) |
| 171 | +(defn- get-connector-thread-options [options] |
| 172 | + (let [acceptors (options :acceptor-threads |
| 173 | + (when (:virtual-pool? options) |
| 174 | + 1)) |
| 175 | + selectors (options :selector-threads |
| 176 | + (when acceptors |
| 177 | + (-> (Runtime/getRuntime) (.availableProcessors))))] |
| 178 | + {:acceptors acceptors |
| 179 | + :selectors selectors})) |
| 180 | + |
| 181 | +(defn- server-connector ^ServerConnector [^Server server factories options] |
| 182 | + (let [{:keys [acceptors |
| 183 | + selectors]} (get-connector-thread-options options)] |
| 184 | + (if (and acceptors selectors) |
| 185 | + (ServerConnector. server (int acceptors) (int selectors) |
| 186 | + ^{:tag "[Lorg.eclipse.jetty.server.ConnectionFactory;"} |
| 187 | + (into-array ConnectionFactory factories)) |
| 188 | + (ServerConnector. server |
| 189 | + ^{:tag "[Lorg.eclipse.jetty.server.ConnectionFactory;"} |
| 190 | + (into-array ConnectionFactory factories))))) |
174 | 191 |
|
175 | 192 | (defn- unix-domain-server-connector ^UnixDomainServerConnector
|
176 |
| - [^Server server & factories] |
177 |
| - (UnixDomainServerConnector. server #^"[Lorg.eclipse.jetty.server.ConnectionFactory;" |
178 |
| - (into-array ConnectionFactory factories))) |
| 193 | + [^Server server factories options] |
| 194 | + (let [{:keys [acceptors |
| 195 | + selectors]} (get-connector-thread-options options)] |
| 196 | + (if (and acceptors selectors) |
| 197 | + (UnixDomainServerConnector. server (int acceptors) (int selectors) |
| 198 | + ^{:tag "[Lorg.eclipse.jetty.server.ConnectionFactory;"} |
| 199 | + (into-array ConnectionFactory factories)) |
| 200 | + (UnixDomainServerConnector. server |
| 201 | + ^{:tag "[Lorg.eclipse.jetty.server.ConnectionFactory;"} |
| 202 | + (into-array ConnectionFactory factories))))) |
179 | 203 |
|
180 | 204 | (defn- http-config ^HttpConfiguration [options]
|
181 | 205 | (doto (HttpConfiguration.)
|
|
187 | 211 |
|
188 | 212 | (defn- http-connector ^ServerConnector [server options]
|
189 | 213 | (let [http-factory (HttpConnectionFactory. (http-config options))]
|
190 |
| - (doto (server-connector server http-factory) |
| 214 | + (doto (server-connector server [http-factory] options) |
191 | 215 | (.setPort (options :port 80))
|
192 | 216 | (.setHost (options :host))
|
193 | 217 | (.setIdleTimeout (options :max-idle-time 200000)))))
|
|
239 | 263 | (when-let [scan-interval (options :keystore-scan-interval)]
|
240 | 264 | (.addBean server (doto (KeyStoreScanner. ssl-context)
|
241 | 265 | (.setScanInterval scan-interval))))
|
242 |
| - (doto (server-connector server ssl-factory http-factory) |
| 266 | + (doto (server-connector server [ssl-factory http-factory] options) |
243 | 267 | (.setPort ssl-port)
|
244 | 268 | (.setHost (options :host))
|
245 | 269 | (.setIdleTimeout (options :max-idle-time 200000)))))
|
|
248 | 272 | (let [http-factory (HttpConnectionFactory. (http-config options))
|
249 | 273 | socket (io/file (options :unix-socket))]
|
250 | 274 | (.deleteOnExit socket)
|
251 |
| - (doto (unix-domain-server-connector server http-factory) |
| 275 | + (doto (unix-domain-server-connector server [http-factory] options) |
252 | 276 | (.setUnixDomainPath (.toPath socket))
|
253 | 277 | (.setIdleTimeout (options :max-idle-time 200000)))))
|
254 | 278 |
|
|
269 | 293 | (.setDaemon pool true))
|
270 | 294 | pool))
|
271 | 295 |
|
| 296 | +(defn- create-virtual-threadpool [options] |
| 297 | + (let [max-threads (options :max-threads 50) |
| 298 | + pool (VirtualThreadPool. max-threads)] |
| 299 | + pool)) |
| 300 | + |
272 | 301 | (defn- create-server ^Server [options]
|
273 |
| - (let [pool (or (:thread-pool options) (create-threadpool options)) |
| 302 | + (let [pool (or (:thread-pool options) |
| 303 | + (when (:virtual-pool? options false) |
| 304 | + (create-virtual-threadpool options)) |
| 305 | + (create-threadpool options)) |
274 | 306 | server (Server. ^ThreadPool pool)]
|
275 | 307 | (when (:http? options true)
|
276 | 308 | (.addConnector server (http-connector server options)))
|
|
317 | 349 | :keystore-scan-interval - if not nil, the interval in seconds to scan for an
|
318 | 350 | updated keystore
|
319 | 351 | :thread-pool - custom thread pool instance for Jetty to use
|
| 352 | + :virtual-pool? - use a VirtualThreadPool (requires Java 19+) |
320 | 353 | :truststore - a truststore to use for SSL connections
|
321 | 354 | :trust-password - the password to the truststore
|
| 355 | + :acceptor-threads - the number of acceptor threads to use |
| 356 | + (default 1 if using virtual-pool?, otherwise not set) |
| 357 | + :selector-threads - the number of selector threads to use |
| 358 | + (default to available processors if using virtual-pool?, otherwise not set) |
322 | 359 | :max-threads - the maximum number of threads to use (default 50)
|
323 | 360 | :min-threads - the minimum number of threads to use (default 8)
|
324 | 361 | :max-queued-requests - the maximum number of requests to be queued
|
|
0 commit comments