public final boolean isExpired() { return isExpired(System.currentTimeMillis()); }
public ThreadSafeAsyncHttpClient(final String collectorHost, final int collectorPort, final EventType eventType, final long httpMaxKeepAliveInMillis) { this.collectorURI = String.format("http://%s:%d%s", collectorHost, collectorPort, URI_PATH); this.eventType = eventType; this.httpConnectionExpiration = new ExpirationTimer(httpMaxKeepAliveInMillis); // CAUTION: it is not enforced that the actual event encoding type on the wire matches what the config says it is // the event encoding type is determined by the Event's writeExternal() method. this.clientConfig = new AsyncHttpClientConfig.Builder() .setIdleConnectionInPoolTimeoutInMs(DEFAULT_IDLE_CONNECTION_IN_POOL_TIMEOUT_IN_MS) .setConnectionTimeoutInMs(100) .setMaximumConnectionsPerHost(-1) // unlimited connections .build(); }
Request createPostRequest(final File file) { AsyncHttpClient.BoundRequestBuilder requestBuilder = client .preparePost(collectorURI).setBody(file) .setHeader("Content-Type", headers.get(eventType)); // zero-bytes-copy /* * Need to ensure we won't be using a single connection indefinitely, * to ensure load balancing works. */ if (httpConnectionExpiration.isExpired()) { requestBuilder = requestBuilder.setHeader("Connection", "close"); } return requestBuilder.build(); } }