Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shutdown hook to close AsyncHttpClient. #8763

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add shutdown hook to close AsyncHttpClient. Add Threadfactory to Asyn…
…cHttpClient to update threadfactory to use daemon threads.
  • Loading branch information
pujagani committed Oct 7, 2020
commit 930b9f45de278df435e2d8f485bbadfd0313962b
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ java_library(
artifact("io.netty:netty-transport-native-kqueue"),
artifact("io.netty:netty-transport-native-kqueue-osx-x86_64"),
artifact("io.netty:netty-transport-native-unix-common"),
artifact("io.netty:netty-common"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.openqa.selenium.remote.http.netty;

import com.google.auto.service.AutoService;

import io.netty.util.concurrent.DefaultThreadFactory;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Dsl;
Expand All @@ -32,16 +32,20 @@
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.WebSocket;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.io.IOException;

public class NettyClient implements HttpClient {

private static final AsyncHttpClient httpClient = Dsl.asyncHttpClient(
new DefaultAsyncHttpClientConfig.Builder()
.setUseInsecureTrustManager(true)
.setAggregateWebSocketFrameFragments(true)
.setWebSocketMaxBufferSize(Integer.MAX_VALUE)
.setWebSocketMaxFrameSize(Integer.MAX_VALUE));
private static final AsyncHttpClient httpClient =
Dsl.asyncHttpClient(
new DefaultAsyncHttpClientConfig.Builder()
.setThreadFactory(new DefaultThreadFactory("AsyncHttpClient", true))
.setUseInsecureTrustManager(true)
.setAggregateWebSocketFrameFragments(true)
.setWebSocketMaxBufferSize(Integer.MAX_VALUE)
.setWebSocketMaxFrameSize(Integer.MAX_VALUE));

private final HttpHandler handler;
private BiFunction<HttpRequest, WebSocket.Listener, WebSocket> toWebSocket;
Expand Down Expand Up @@ -76,6 +80,23 @@ public HttpClient with(Filter filter) {
@HttpClientName("netty")
public static class Factory implements HttpClient.Factory {

private static final AtomicBoolean addedHook = new AtomicBoolean();

public Factory() {
if (!addedHook.get()) {
Runtime.getRuntime().addShutdownHook(new Thread(this::callAsyncClientShutdown));
addedHook.set(true);
}
}

private void callAsyncClientShutdown() {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public HttpClient createClient(ClientConfig config) {
Require.nonNull("Client config", config);
Expand Down