Skip to content

Commit b1dfc1f

Browse files
Bug/stale connection error (#155)
* fix for stale connection * add more commenting and remove unnecessary new line * update connection manager values as setable through the constructor
1 parent bf59159 commit b1dfc1f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/event/AsyncEventHandler.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.http.entity.StringEntity;
3030
import org.apache.http.impl.client.CloseableHttpClient;
3131
import org.apache.http.impl.client.HttpClients;
32+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
3233
import org.slf4j.Logger;
3334
import org.slf4j.LoggerFactory;
3435

@@ -50,6 +51,15 @@
5051
*/
5152
public class AsyncEventHandler implements EventHandler, Closeable {
5253

54+
// The following static values are public so that they can be tweaked if necessary.
55+
// These are the recommended settings for http protocol. https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
56+
// The maximum number of connections allowed across all routes.
57+
private int maxTotalConnections = 200;
58+
// The maximum number of connections allowed for a route
59+
private int maxPerRoute = 20;
60+
// Defines period of inactivity in milliseconds after which persistent connections must be re-validated prior to being leased to the consumer.
61+
private int validateAfterInactivity = 5000;
62+
5363
private static final Logger logger = LoggerFactory.getLogger(AsyncEventHandler.class);
5464
private static final ProjectConfigResponseHandler EVENT_RESPONSE_HANDLER = new ProjectConfigResponseHandler();
5565

@@ -58,13 +68,22 @@ public class AsyncEventHandler implements EventHandler, Closeable {
5868
private final BlockingQueue<LogEvent> logEventQueue;
5969

6070
public AsyncEventHandler(int queueCapacity, int numWorkers) {
71+
this(queueCapacity, numWorkers, 200, 20, 5000);
72+
}
73+
74+
public AsyncEventHandler(int queueCapacity, int numWorkers, int maxConnections, int connectionsPerRoute, int validateAfter) {
6175
if (queueCapacity <= 0) {
6276
throw new IllegalArgumentException("queue capacity must be > 0");
6377
}
6478

79+
this.maxTotalConnections = maxConnections;
80+
this.maxPerRoute = connectionsPerRoute;
81+
this.validateAfterInactivity = validateAfter;
82+
6583
this.logEventQueue = new ArrayBlockingQueue<LogEvent>(queueCapacity);
6684
this.httpClient = HttpClients.custom()
6785
.setDefaultRequestConfig(HttpClientUtils.DEFAULT_REQUEST_CONFIG)
86+
.setConnectionManager(poolingHttpClientConnectionManager())
6887
.disableCookieManagement()
6988
.build();
7089

@@ -78,6 +97,15 @@ public AsyncEventHandler(int queueCapacity, int numWorkers) {
7897
}
7998
}
8099

100+
private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager()
101+
{
102+
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
103+
poolingHttpClientConnectionManager.setMaxTotal(maxTotalConnections);
104+
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
105+
poolingHttpClientConnectionManager.setValidateAfterInactivity(validateAfterInactivity);
106+
return poolingHttpClientConnectionManager;
107+
}
108+
81109
@Override
82110
public void dispatchEvent(LogEvent logEvent) {
83111
// attempt to enqueue the log event for processing

0 commit comments

Comments
 (0)