Skip to content

Commit

Permalink
Grouping Executors related methods into a single class (helidon-io#9271)
Browse files Browse the repository at this point in the history
* Grouping Executors related methods into a single class


Signed-off-by: Tomas Langer <[email protected]>
Co-authored-by: Tomas Langer <[email protected]>
  • Loading branch information
JaroslavTulach and tomas-langer authored Sep 23, 2024
1 parent ea72a61 commit 6e9b457
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.webserver;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import io.helidon.common.task.HelidonTaskExecutor;

/**
* Encapsulates operations with {@link Executors}. Helps to workaround
* limitations of GraalVM for JDK21 which doesn't support execution of
* virtual threads and Graal.js code together. New versions of GraalVM
* don't have this limitation, but for those who stick with JDK21, this
* is a serious limitations in using Helidon 4.0.x
* <p>
* By moving these <em>"factories"</em> into separate class, it is easier
* to use GraalVM's `@Substitute` mechanism and get Helidon and Graal.js working
* on GraalVM for JDK21. More info
* <a href="https://github.com/enso-org/enso/pull/10783#discussion_r1768000821">available in PR-10783</a>.
*/
final class ExecutorsFactory {

private ExecutorsFactory() {
}

/**
* Used by {@link LoomServer} to allocate its executor service.
*
* @return {@link Executors#newVirtualThreadPerTaskExecutor()}
*/
static ExecutorService newLoomServerVirtualThreadPerTaskExecutor() {
return Executors.newVirtualThreadPerTaskExecutor();
}

/**
* Used by {@link ServerListener} to allocate its reader executor.
*
* @return {@link ThreadPerTaskExecutor#create(java.util.concurrent.ThreadFactory)}
*/
static HelidonTaskExecutor newServerListenerReaderExecutor() {
return ThreadPerTaskExecutor.create(virtualThreadFactory());
}

/**
* Used by {@link ServerListener} to allocate its shared executor.
*
* @return {@link Executors#newThreadPerTaskExecutor(java.util.concurrent.ThreadFactory)}.
*/
static ExecutorService newServerListenerSharedExecutor() {
return Executors.newThreadPerTaskExecutor(virtualThreadFactory());
}

private static ThreadFactory virtualThreadFactory() {
return Thread.ofVirtual().factory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Timer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -71,7 +70,7 @@ class LoomServer implements WebServer {
.id("web-" + WEBSERVER_COUNTER.getAndIncrement())
.build());
this.serverConfig = serverConfig;
this.executorService = Executors.newVirtualThreadPerTaskExecutor();
this.executorService = ExecutorsFactory.newLoomServerVirtualThreadPerTaskExecutor();

Map<String, ListenerConfig> sockets = new HashMap<>(serverConfig.sockets());
sockets.put(DEFAULT_SOCKET_NAME, serverConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -143,12 +142,10 @@ class ServerListener implements ListenerContext {
.unstarted(this::listen);

// to read requests and execute tasks
this.readerExecutor = ThreadPerTaskExecutor.create(Thread.ofVirtual()
.factory());
this.readerExecutor = ExecutorsFactory.newServerListenerReaderExecutor();

// to do anything else (writers etc.)
this.sharedExecutor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual()
.factory());
this.sharedExecutor = ExecutorsFactory.newServerListenerSharedExecutor();

this.closeFuture = new CompletableFuture<>();

Expand Down

0 comments on commit 6e9b457

Please sign in to comment.