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

Reuse HttpClient to pool connections #954

Merged
merged 2 commits into from
Oct 11, 2022
Merged
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
36 changes: 17 additions & 19 deletions src/main/java/fi/aalto/cs/apluscourses/utils/CoursesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
Expand All @@ -24,7 +24,6 @@
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.VersionInfo;
Expand All @@ -44,6 +43,8 @@ public class CoursesClient {

private static final AtomicReference<String> userAgent = new AtomicReference<>(null);

private static HttpClient httpClient;

/**
* Makes a GET request to the given URL and returns the response body in a
* {@link ByteArrayInputStream}.
Expand Down Expand Up @@ -213,11 +214,9 @@ public static <T> T post(@NotNull URL url,
authentication.addToRequest(request);
}

try (CloseableHttpClient client = createClient();
CloseableHttpResponse response = client.execute(request)) {
requireSuccessStatusCode(response);
return mapper.map(response);
}
HttpResponse response = createClient().execute(request);
requireSuccessStatusCode(response);
return mapper.map(response);
}

@NotNull
Expand All @@ -240,11 +239,9 @@ private static ContentBody getContentBody(@NotNull Object value) {
*/
private static <T> T mapResponse(@NotNull HttpUriRequest request,
@NotNull ResponseMapper<T> mapper) throws IOException {
try (CloseableHttpClient client = createClient();
CloseableHttpResponse response = client.execute(request)) {
requireSuccessStatusCode(response);
return mapper.map(response);
}
HttpResponse response = createClient().execute(request);
requireSuccessStatusCode(response);
return mapper.map(response);
}

/**
Expand All @@ -253,11 +250,9 @@ private static <T> T mapResponse(@NotNull HttpUriRequest request,
*/
private static void consumeResponse(@NotNull HttpUriRequest request,
@NotNull ResponseConsumer consumer) throws IOException {
try (CloseableHttpClient client = createClient();
CloseableHttpResponse response = client.execute(request)) {
requireSuccessStatusCode(response);
consumer.consume(response);
}
HttpResponse response = createClient().execute(request);
requireSuccessStatusCode(response);
consumer.consume(response);
}

/**
Expand Down Expand Up @@ -309,8 +304,11 @@ private static void requireResponseEntity(@NotNull HttpResponse response)
}

@NotNull
private static CloseableHttpClient createClient() {
return HttpClients.custom().setUserAgent(getUserAgent()).build();
private static synchronized HttpClient createClient() {
Taikelenn marked this conversation as resolved.
Show resolved Hide resolved
if (httpClient == null) {
httpClient = HttpClients.custom().setUserAgent(getUserAgent()).build();
}
return httpClient;
}

@NotNull
Expand Down