Skip to content

Commit

Permalink
[grid] Add thread pool for driver service startup
Browse files Browse the repository at this point in the history
Driver service startup uses CompletableFuture that uses the ForkJoinPool's common pool by default. This might have performance-related implications when sessions run in parallel. The changes add a fixed thread pool that is used by the CompletableFuture.
  • Loading branch information
pujagani committed May 13, 2021
1 parent da5331b commit 49f706f
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.os.CommandLine;
import org.openqa.selenium.os.ExecutableFinder;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -42,6 +43,8 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -55,8 +58,15 @@
* In addition to this, it is supposed that the driver server implements /shutdown hook that is
* used to stop the server.
*/
public class DriverService {
public class DriverService implements Closeable {
protected static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(20);
private final ExecutorService executorService = Executors.newFixedThreadPool(2, r -> {
Thread thread = new Thread(r);
thread.setName("Driver Service Executor");
thread.setDaemon(true);
return thread;
});


/**
* The base URL for the managed server.
Expand Down Expand Up @@ -198,7 +208,7 @@ public void start() throws IOException {
CompletableFuture<StartOrDie> serverStarted = CompletableFuture.supplyAsync(() -> {
waitUntilAvailable();
return StartOrDie.SERVER_STARTED;
});
}, executorService);

CompletableFuture<StartOrDie> processFinished = CompletableFuture.supplyAsync(() -> {
try {
Expand All @@ -207,7 +217,7 @@ public void start() throws IOException {
return StartOrDie.PROCESS_IS_ACTIVE;
}
return StartOrDie.PROCESS_DIED;
});
}, executorService);

try {
StartOrDie status = (StartOrDie) CompletableFuture.anyOf(serverStarted, processFinished)
Expand Down Expand Up @@ -302,6 +312,11 @@ protected OutputStream getOutputStream() {
return outputStream;
}

@Override
public void close() {
executorService.shutdownNow();
}

public abstract static class Builder<DS extends DriverService, B extends Builder<?, ?>> {

private int port = 0;
Expand Down

0 comments on commit 49f706f

Please sign in to comment.