Skip to content

Commit

Permalink
[java] Reusing the same HTTP client instead of creating a client per …
Browse files Browse the repository at this point in the history
…session. Fixes #7989
  • Loading branch information
barancev committed Mar 10, 2020
1 parent 264ed97 commit aeb6175
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 56 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.remote.http.netty;

import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Dsl;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpClient;
Expand All @@ -30,6 +32,8 @@

public class NettyClient implements HttpClient {

private final static AsyncHttpClient httpClient = Dsl.asyncHttpClient();

private final HttpHandler handler;
private BiFunction<HttpRequest, WebSocket.Listener, WebSocket> toWebSocket;

Expand Down Expand Up @@ -65,7 +69,7 @@ public static class Factory implements HttpClient.Factory {
public HttpClient createClient(ClientConfig config) {
Objects.requireNonNull(config, "Client config to use must be set.");

return new NettyClient(new NettyHttpHandler(config).with(config.filter()), NettyWebSocket.create(config));
return new NettyClient(new NettyHttpHandler(config, httpClient).with(config.filter()), NettyWebSocket.create(config, httpClient));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

public class NettyHttpHandler extends RemoteCall {

private final AsyncHttpClient client;
private final HttpHandler handler;
private final AsyncHttpClient client;

public NettyHttpHandler(ClientConfig config) {
public NettyHttpHandler(ClientConfig config, AsyncHttpClient client) {
super(config);
this.client = new CreateNettyClient().apply(config);
this.client = client;
this.handler = config.filter().andFinally(this::makeCall);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.openqa.selenium.remote.http.Contents.empty;

import org.asynchttpclient.Dsl;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;
Expand All @@ -31,6 +32,8 @@

import static org.asynchttpclient.Dsl.request;

import com.google.common.base.Strings;

class NettyMessages {

private NettyMessages() {
Expand Down Expand Up @@ -64,6 +67,15 @@ protected static Request toNettyRequest(URI baseUrl, HttpRequest request) {
}
}

String info = baseUrl.getUserInfo();
if (!Strings.isNullOrEmpty(info)) {
String[] parts = info.split(":", 2);
String user = parts[0];
String pass = parts.length > 1 ? parts[1] : null;

builder.setRealm(Dsl.basicAuthRealm(user, pass).setUsePreemptiveAuth(true).build());
}

if (request.getMethod().equals(HttpMethod.POST)) {
builder.setBody(request.getContent().get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NettyWebSocket implements WebSocket {

private static final Logger log = Logger.getLogger(NettyWebSocket.class.getName());

private org.asynchttpclient.ws.WebSocket socket;
private final org.asynchttpclient.ws.WebSocket socket;

private NettyWebSocket(AsyncHttpClient client, org.asynchttpclient.Request request, Listener listener) {
Objects.requireNonNull(client, "HTTP client to use must be set.");
Expand Down Expand Up @@ -97,7 +97,7 @@ public void onTextFrame(String payload, boolean finalFragment, int rsv) {
}
}

static BiFunction<HttpRequest, Listener, WebSocket> create(ClientConfig config) {
static BiFunction<HttpRequest, Listener, WebSocket> create(ClientConfig config, AsyncHttpClient client) {
Filter filter = config.filter();

Function<HttpRequest, HttpRequest> filterRequest = req -> {
Expand All @@ -109,7 +109,6 @@ static BiFunction<HttpRequest, Listener, WebSocket> create(ClientConfig config)
return ref.get();
};

AsyncHttpClient client = new CreateNettyClient().apply(config);
return (req, listener) -> {
HttpRequest filtered = filterRequest.apply(req);

Expand Down

0 comments on commit aeb6175

Please sign in to comment.