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

upgrade apache httpclient from 4.x to 5.x #2729

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions clients/java/msd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${apache.httpclient.version}</version>
</dependency>
<dependency>
Expand Down
67 changes: 38 additions & 29 deletions clients/java/msd/src/main/java/com/yahoo/athenz/msd/MSDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import java.util.Map;
import javax.net.ssl.SSLContext;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
import org.apache.hc.core5.pool.PoolReusePolicy;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +42,9 @@ public class MSDClient implements Closeable {
public static final String MSD_CLIENT_PROP_READ_TIMEOUT = "athenz.msd.client.read_timeout";
public static final String MSD_CLIENT_PROP_CONNECT_TIMEOUT = "athenz.msd.client.connect_timeout";
public static final String MSD_CLIENT_PROP_MSD_URL = "athenz.msd.client.msd_url";
public static final String MSD_CLIENT_PROP_POOL_MAX_PER_ROUTE = "athenz.msd.client.http_pool_max_per_route";
public static final String MSD_CLIENT_PROP_POOL_MAX_TOTAL = "athenz.msd.client.http_pool_max_total";
public static final String MSD_CLIENT_PROP_TIME_TO_LIVE = "athenz.msd.client.http_pool_time_to_live";

protected MSDRDLGeneratedClient client = null;

Expand All @@ -64,25 +70,33 @@ public MSDClient(String url, SSLContext sslContext) {
}

protected PoolingHttpClientConnectionManager createConnectionPooling(SSLContext sslContext) {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", new SSLConnectionSocketFactory(sslContext))
.register("http", new PlainConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);

//route is host + port. Since we have only one, set the max and the route the same
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(30);
poolingHttpClientConnectionManager.setMaxTotal(20);
return poolingHttpClientConnectionManager;
int maxPerRoute = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_POOL_MAX_PER_ROUTE, "2"));
int maxTotal = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_POOL_MAX_TOTAL, "20"));
int readTimeout = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_READ_TIMEOUT, "30000"));
int connectTimeout = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_CONNECT_TIMEOUT, "30000"));
int timeToLive = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_TIME_TO_LIVE, "10"));

return PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(sslContext)
.setTlsVersions(TLS.V_1_3)
.build())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
.setConnPoolPolicy(PoolReusePolicy.LIFO)
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setSocketTimeout(Timeout.ofMilliseconds(readTimeout))
.setConnectTimeout(Timeout.ofMilliseconds(connectTimeout))
.setTimeToLive(TimeValue.ofMinutes(timeToLive))
.build())
.setMaxConnPerRoute(maxPerRoute)
.setMaxConnTotal(maxTotal)
.build();
}

protected CloseableHttpClient createHttpClient(int connTimeoutMs, int readTimeoutMs,
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager) {
protected CloseableHttpClient createHttpClient(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager) {

//apache http client expects in milliseconds
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(connTimeoutMs)
.setSocketTimeout(readTimeoutMs)
.setRedirectsEnabled(false)
.build();
return HttpClients.custom()
Expand Down Expand Up @@ -110,13 +124,8 @@ private void initClient(String url, SSLContext sslContext) {
msdUrl = url;
}

// determine our read and connect timeouts

int readTimeout = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_READ_TIMEOUT, "30000"));
int connectTimeout = Integer.parseInt(System.getProperty(MSD_CLIENT_PROP_CONNECT_TIMEOUT, "30000"));

PoolingHttpClientConnectionManager connManager = createConnectionPooling(sslContext);
CloseableHttpClient httpClient = createHttpClient(connectTimeout, readTimeout, connManager);
CloseableHttpClient httpClient = createHttpClient(connManager);

client = new MSDRDLGeneratedClient(msdUrl, httpClient);
}
Expand Down
Loading
Loading