Skip to content

Commit

Permalink
Add connection pooling to http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Nov 3, 2024
1 parent eb358c2 commit c205d05
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
Expand All @@ -35,6 +36,7 @@
import java.util.stream.Stream;

import static edu.harvard.dbmi.avillach.service.ResourceWebClient.QUERY_METADATA_FIELD;
import static edu.harvard.dbmi.avillach.util.HttpClientUtil.getConfiguredHttpClient;
import static edu.harvard.dbmi.avillach.util.HttpClientUtil.readObjectFromResponse;

@Path("/aggregate-data-sharing")
Expand Down Expand Up @@ -66,6 +68,8 @@ public class AggregateDataSharingResourceRS implements IResourceRS {

private final String randomSalt;

private final HttpClientUtil httpClientUtil;

public AggregateDataSharingResourceRS() {
this(null);
}
Expand All @@ -89,6 +93,11 @@ public AggregateDataSharingResourceRS(ApplicationProperties applicationPropertie
randomSalt = properties.getTargetPicsureObfuscationSalt();

headers = new Header[]{new BasicHeader(HttpHeaders.AUTHORIZATION, BEARER_STRING + properties.getTargetPicsureToken())};

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100); // Maximum total connections
connectionManager.setDefaultMaxPerRoute(20); // Maximum connections per route
httpClientUtil = HttpClientUtil.getInstance(connectionManager);
}


Expand Down Expand Up @@ -120,8 +129,8 @@ public ResourceInfo info(QueryRequest infoRequest) {

String payload = objectMapper.writeValueAsString(chainRequest);
String composedURL = HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName);
HttpResponse response = HttpClientUtil.retrievePostResponse(composedURL, headers, payload);
if (!HttpClientUtil.is2xx(response)) {
HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, payload);
if (!httpClientUtil.is2xx(response)) {
logger.error("{}{} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName,
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
HttpClientUtil.throwResponseError(response, properties.getTargetPicsureUrl());
Expand Down Expand Up @@ -194,7 +203,7 @@ private HttpResponse postRequest(QueryRequest statusRequest, String pathName) {
QueryRequest chainRequest = createChainRequest(statusRequest);
String payload = objectMapper.writeValueAsString(chainRequest);
String composedURL = HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName);
HttpResponse response = HttpClientUtil.retrievePostResponse(composedURL, headers, payload);
HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, payload);
if (!HttpClientUtil.is2xx(response)) {
logger.error("{}{} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName,
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
Expand Down Expand Up @@ -267,7 +276,7 @@ private HttpResponse getHttpResponse(QueryRequest queryRequest, UUID resourceUUI
String composedURL = HttpClientUtil.composeURL(targetPicsureUrl, pathName);

logger.debug("Aggregate Data Sharing Resource, sending query: " + queryString + ", to: " + composedURL);
HttpResponse response = HttpClientUtil.retrievePostResponse(composedURL, headers, queryString);
HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, queryString);
if (!HttpClientUtil.is2xx(response)) {
logger.error("Not 200 status!");
logger.error(
Expand Down Expand Up @@ -414,7 +423,7 @@ public Response queryFormat(QueryRequest queryRequest) {
try {
String queryString = objectMapper.writeValueAsString(queryRequest);
String composedURL = HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName);
HttpResponse response = HttpClientUtil.retrievePostResponse(composedURL, headers, queryString);
HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, queryString);
if (!HttpClientUtil.is2xx(response)) {
logger.error(
composedURL + " calling resource with id " + resourceUUID + " did not return a 200: {} {} ",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import edu.harvard.dbmi.avillach.util.HttpClientUtil;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,14 +37,24 @@ public class PassThroughResourceRS implements IResourceRS {
@Inject
private ApplicationProperties properties;
@Inject
private HttpClient httpClient;
private HttpClientUtil httpClient;

public PassThroughResourceRS() {
}

@Inject
public PassThroughResourceRS(ApplicationProperties applicationProperties, HttpClient httpClient) {
public PassThroughResourceRS(ApplicationProperties applicationProperties) {
this.properties = applicationProperties;

PoolingHttpClientConnectionManager connectionManager;
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100); // Maximum total connections
connectionManager.setDefaultMaxPerRoute(20); // Maximum connections per route
this.httpClient = HttpClientUtil.getInstance(connectionManager);
}

public PassThroughResourceRS(ApplicationProperties properties, HttpClientUtil httpClient) {
this.properties = properties;
this.httpClient = httpClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.UUID;

import edu.harvard.dbmi.avillach.util.HttpClientUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
Expand Down Expand Up @@ -41,7 +42,7 @@
@ExtendWith(MockitoExtension.class)
class PassThroughResourceRSTest {

HttpClient httpClient;
HttpClientUtil httpClient;
PassThroughResourceRS resource;
ObjectMapper objectMapper = new ObjectMapper();

Expand All @@ -52,12 +53,7 @@ void init() {
lenient().when(appProperties.getTargetPicsureUrl()).thenReturn("http://test");
lenient().when(appProperties.getTargetResourceId()).thenReturn(UUID.randomUUID().toString());

httpClient = mock(HttpClient.class);
// not mocking these methods...
lenient().doCallRealMethod().when(httpClient).composeURL(anyString(), anyString());
lenient().doCallRealMethod().when(httpClient).readObjectFromResponse(any(HttpResponse.class), any());
lenient().doCallRealMethod().when(httpClient).throwResponseError(any(HttpResponse.class), anyString());
lenient().doCallRealMethod().when(httpClient).throwInternalResponseError(any(HttpResponse.class), anyString());
httpClient = mock(HttpClientUtil.class);

resource = new PassThroughResourceRS(appProperties, httpClient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,7 +34,13 @@ public class ProxyWebClient {
ResourceRepository resourceRepository;

public ProxyWebClient() {
client = HttpClientUtil.getConfiguredHttpClient();
PoolingHttpClientConnectionManager connectionManager;

connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100); // Maximum total connections
connectionManager.setDefaultMaxPerRoute(20); // Maximum connections per route

client = HttpClientUtil.getConfiguredHttpClient(connectionManager);
}

public Response postProxy(
Expand Down
Loading

0 comments on commit c205d05

Please sign in to comment.