Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.launchdarkly.sdk.internal.http.HttpHelpers;
import com.launchdarkly.sdk.internal.http.HttpProperties;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
Expand Down Expand Up @@ -45,9 +47,15 @@ final class AnalyticsEventSender implements Closeable {
private final OkHttpClient client;
private final boolean ownsClient;
private final Headers baseHeaders;
private final boolean compress;
private final LDLogger logger;

AnalyticsEventSender(HttpProperties httpProperties, LDLogger logger) {
/**
* @param compress whether to send bodies gzip-encoded; the batch on disk stays plain JSON, and each
* attempt compresses it again
*/
AnalyticsEventSender(HttpProperties httpProperties, boolean compress, LDLogger logger) {
this.compress = compress;
this.logger = logger;
if (httpProperties.getSharedHttpClient() == null) {
this.client = httpProperties.toHttpClientBuilder().build();
Expand Down Expand Up @@ -97,14 +105,20 @@ static final class Result {
Result sendBatch(EventLogFormat.Payload payload, int eventCount, String payloadId, URI eventsBaseUri) {
URI uri = HttpHelpers.concatenateUriPath(eventsBaseUri,
StandardEndpoints.ANALYTICS_EVENTS_REQUEST_PATH);
Request request = new Request.Builder()
Request.Builder builder = new Request.Builder()
.url(uri.toString())
.headers(baseHeaders)
.addHeader("Content-Type", "application/json")
.addHeader(EVENT_SCHEMA_HEADER, EVENT_SCHEMA_VERSION)
.addHeader(PAYLOAD_ID_HEADER, payloadId)
.post(RequestBody.create(payload.bytes, JSON, 0, payload.length))
.build();
.addHeader(PAYLOAD_ID_HEADER, payloadId);
byte[] gzipped = compress ? gzip(payload) : null;
if (gzipped != null) {
builder.addHeader("Content-Encoding", "gzip")
.post(RequestBody.create(gzipped, JSON));
} else {
builder.post(RequestBody.create(payload.bytes, JSON, 0, payload.length));
}
Request request = builder.build();

try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
Expand All @@ -125,6 +139,18 @@ Result sendBatch(EventLogFormat.Payload payload, int eventCount, String payloadI
}
}

/** The body gzip-encoded, or null where encoding failed and it goes as it is. */
private byte[] gzip(EventLogFormat.Payload payload) {
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(32, payload.length / 4));
try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
gzip.write(payload.bytes, 0, payload.length);
} catch (IOException e) {
logger.warn("Could not compress events, sending them uncompressed: {}", LogValues.exceptionSummary(e));
return null;
}
return out.toByteArray();
}

/** {@code Retry-After} as seconds or as an HTTP date, or -1 where it is absent or unreadable. */
private static long retryAfterMillis(Response response) {
String value = response.header("Retry-After");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public EventProcessor build(ClientContext clientContext) {
makeEventStore(clientContext, clientContextImpl),
diagnosticEventSender,
new AnalyticsEventSender(LDUtil.makeHttpProperties(clientContext),
clientContext.getBaseLogger()),
enableCompression, clientContext.getBaseLogger()),
makeRetryState(),
clientContext.getServiceEndpoints().getEventsBaseUri(),
clientContextImpl.getDiagnosticStore(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4836,7 +4836,7 @@ private DirectEventProcessor makeEventProcessor(EventSender diagnosticSender,
diagnosticSender,
new AnalyticsEventSender(LDUtil.makeHttpProperties(
new HttpConfiguration(2000, Collections.emptyMap(), null, false)),
logging.logger),
false, logging.logger),
lastRetryState,
eventsUri,
diagnosticStore,
Expand Down
Loading