diff options
author | 2022-12-10 14:32:17 +0100 | |
---|---|---|
committer | 2022-12-12 16:31:04 +0100 | |
commit | d786792fcf32eb5d94c870a83020941b8693deb1 (patch) | |
tree | ea8b5a57468cb24f864859639bb537d46354823f /subprojects/language-web/src/main | |
parent | refactor(frontend): split vite config (diff) | |
download | refinery-d786792fcf32eb5d94c870a83020941b8693deb1.tar.gz refinery-d786792fcf32eb5d94c870a83020941b8693deb1.tar.zst refinery-d786792fcf32eb5d94c870a83020941b8693deb1.zip |
chore(web): upgrade to Jetty 12.0.0.alpha3
Also refactor virtual thread ExecutorService handling and integration
tests.
Diffstat (limited to 'subprojects/language-web/src/main')
3 files changed, 55 insertions, 22 deletions
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java index 960eef71..f49f46ee 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java | |||
@@ -11,7 +11,6 @@ import org.eclipse.jetty.ee10.servlet.ServletHolder; | |||
11 | import org.eclipse.jetty.ee10.servlet.SessionHandler; | 11 | import org.eclipse.jetty.ee10.servlet.SessionHandler; |
12 | import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketServletContainerInitializer; | 12 | import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketServletContainerInitializer; |
13 | import org.eclipse.jetty.server.Server; | 13 | import org.eclipse.jetty.server.Server; |
14 | import org.eclipse.jetty.util.VirtualThreads; | ||
15 | import org.eclipse.jetty.util.resource.Resource; | 14 | import org.eclipse.jetty.util.resource.Resource; |
16 | import org.eclipse.jetty.util.resource.ResourceFactory; | 15 | import org.eclipse.jetty.util.resource.ResourceFactory; |
17 | import org.slf4j.Logger; | 16 | import org.slf4j.Logger; |
@@ -44,13 +43,7 @@ public class ServerLauncher { | |||
44 | private final Server server; | 43 | private final Server server; |
45 | 44 | ||
46 | public ServerLauncher(InetSocketAddress bindAddress, String[] allowedOrigins, String webSocketUrl) { | 45 | public ServerLauncher(InetSocketAddress bindAddress, String[] allowedOrigins, String webSocketUrl) { |
47 | server = new Server(bindAddress); | 46 | server = VirtualThreadUtils.newServerWithVirtualThreadsThreadPool("jetty", bindAddress); |
48 | enableVirtualThreads(server); | ||
49 | if (server.getThreadPool() instanceof VirtualThreads.Configurable virtualThreadsConfigurable) { | ||
50 | // Change this to setVirtualThreadsExecutor once | ||
51 | // https://github.com/eclipse/jetty.project/commit/83154b4ffe4767ef44981598d6c26e6a5d32e57c gets released. | ||
52 | virtualThreadsConfigurable.setUseVirtualThreads(VirtualThreads.areSupported()); | ||
53 | } | ||
54 | var handler = new ServletContextHandler(); | 47 | var handler = new ServletContextHandler(); |
55 | addSessionHandler(handler); | 48 | addSessionHandler(handler); |
56 | addProblemServlet(handler, allowedOrigins); | 49 | addProblemServlet(handler, allowedOrigins); |
@@ -150,14 +143,6 @@ public class ServerLauncher { | |||
150 | } | 143 | } |
151 | } | 144 | } |
152 | 145 | ||
153 | public static void enableVirtualThreads(Server server) { | ||
154 | if (server.getThreadPool() instanceof VirtualThreads.Configurable virtualThreadsConfigurable) { | ||
155 | // Change this to setVirtualThreadsExecutor once | ||
156 | // https://github.com/eclipse/jetty.project/commit/83154b4ffe4767ef44981598d6c26e6a5d32e57c gets released. | ||
157 | virtualThreadsConfigurable.setUseVirtualThreads(VirtualThreads.areSupported()); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | private static String getListenAddress() { | 146 | private static String getListenAddress() { |
162 | var listenAddress = System.getenv("LISTEN_ADDRESS"); | 147 | var listenAddress = System.getenv("LISTEN_ADDRESS"); |
163 | if (listenAddress == null) { | 148 | if (listenAddress == null) { |
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/VirtualThreadUtils.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/VirtualThreadUtils.java new file mode 100644 index 00000000..a055e755 --- /dev/null +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/VirtualThreadUtils.java | |||
@@ -0,0 +1,52 @@ | |||
1 | package tools.refinery.language.web; | ||
2 | |||
3 | import org.eclipse.jetty.server.Server; | ||
4 | import org.eclipse.jetty.server.ServerConnector; | ||
5 | import org.eclipse.jetty.util.thread.QueuedThreadPool; | ||
6 | import org.eclipse.jetty.util.thread.ThreadPool; | ||
7 | |||
8 | import java.net.InetSocketAddress; | ||
9 | import java.time.Duration; | ||
10 | import java.util.concurrent.ExecutorService; | ||
11 | import java.util.concurrent.Executors; | ||
12 | |||
13 | public final class VirtualThreadUtils { | ||
14 | private VirtualThreadUtils() { | ||
15 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | ||
16 | } | ||
17 | |||
18 | public static ExecutorService newNamedVirtualThreadsExecutor(String name) { | ||
19 | // Based on | ||
20 | // https://github.com/eclipse/jetty.project/blob/83154b4ffe4767ef44981598d6c26e6a5d32e57c/jetty-server/src/main/config/etc/jetty-threadpool-virtual-preview.xml | ||
21 | return Executors.newThreadPerTaskExecutor(Thread.ofVirtual() | ||
22 | .allowSetThreadLocals(true) | ||
23 | .inheritInheritableThreadLocals(false) | ||
24 | .name(name + "-virtual-", 0) | ||
25 | .factory()); | ||
26 | } | ||
27 | |||
28 | public static ThreadPool newThreadPoolWithVirtualThreadsExecutor(String name) { | ||
29 | // Based on | ||
30 | // https://github.com/eclipse/jetty.project/blob/83154b4ffe4767ef44981598d6c26e6a5d32e57c/jetty-server/src/main/config/etc/jetty-threadpool-virtual-preview.xml | ||
31 | int timeout = (int) Duration.ofMinutes(1).toMillis(); | ||
32 | var threadPool = new QueuedThreadPool(200, 10, timeout, -1, null, null); | ||
33 | threadPool.setName(name); | ||
34 | threadPool.setDetailedDump(false); | ||
35 | threadPool.setVirtualThreadsExecutor(newNamedVirtualThreadsExecutor(name)); | ||
36 | return threadPool; | ||
37 | } | ||
38 | |||
39 | public static Server newServerWithVirtualThreadsThreadPool(String name, InetSocketAddress listenAddress) { | ||
40 | var server = new Server(newThreadPoolWithVirtualThreadsExecutor(name)); | ||
41 | var connector = new ServerConnector(server); | ||
42 | try { | ||
43 | connector.setHost(listenAddress.getHostName()); | ||
44 | connector.setPort(listenAddress.getPort()); | ||
45 | server.addConnector(connector); | ||
46 | } catch (Exception e) { | ||
47 | connector.close(); | ||
48 | throw e; | ||
49 | } | ||
50 | return server; | ||
51 | } | ||
52 | } | ||
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/VirtualThreadExecutorServiceProvider.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/VirtualThreadExecutorServiceProvider.java index ead98927..abbcbd53 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/VirtualThreadExecutorServiceProvider.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/VirtualThreadExecutorServiceProvider.java | |||
@@ -1,9 +1,9 @@ | |||
1 | package tools.refinery.language.web.xtext; | 1 | package tools.refinery.language.web.xtext; |
2 | 2 | ||
3 | import org.eclipse.xtext.ide.ExecutorServiceProvider; | 3 | import org.eclipse.xtext.ide.ExecutorServiceProvider; |
4 | import tools.refinery.language.web.VirtualThreadUtils; | ||
4 | 5 | ||
5 | import java.util.concurrent.ExecutorService; | 6 | import java.util.concurrent.ExecutorService; |
6 | import java.util.concurrent.Executors; | ||
7 | 7 | ||
8 | public class VirtualThreadExecutorServiceProvider extends ExecutorServiceProvider { | 8 | public class VirtualThreadExecutorServiceProvider extends ExecutorServiceProvider { |
9 | private static final String THREAD_POOL_NAME = "xtextWeb"; | 9 | private static final String THREAD_POOL_NAME = "xtextWeb"; |
@@ -11,10 +11,6 @@ public class VirtualThreadExecutorServiceProvider extends ExecutorServiceProvide | |||
11 | @Override | 11 | @Override |
12 | protected ExecutorService createInstance(String key) { | 12 | protected ExecutorService createInstance(String key) { |
13 | var name = key == null ? THREAD_POOL_NAME : THREAD_POOL_NAME + "-" + key; | 13 | var name = key == null ? THREAD_POOL_NAME : THREAD_POOL_NAME + "-" + key; |
14 | return Executors.newThreadPerTaskExecutor(Thread.ofVirtual() | 14 | return VirtualThreadUtils.newNamedVirtualThreadsExecutor(name); |
15 | .allowSetThreadLocals(true) | ||
16 | .inheritInheritableThreadLocals(false) | ||
17 | .name(name + "-", 0) | ||
18 | .factory()); | ||
19 | } | 15 | } |
20 | } | 16 | } |