Skip to content

Commit

Permalink
feat!: bundle the gzip support inside the core SDK (#42)
Browse files Browse the repository at this point in the history
* feat!: bundle the `gzip` support inside the core SDK

Now, GZIP will be enabled by default and have also introduced an API to toggle the GZIP default value, called `enableGZIP`. If user continues to use the `client` API to pass the OkHttpClient then `enableGZIP` API value will not be taken into consideration.

BREAKING CHANGE: Now user who are using the Java SDK, with self-hosted dataPlane and without their own custom `OkHttpClient` (passed using `client` API)` needs to make sure that their server supports GZIP i.e., their server version is above 1.4, else they could either use `enableGZIP` API or pass a custom OkHttpClient instance without the GZIP interceptor.

* refactor: change API name from `enableGZIP` to `setGZIP`
  • Loading branch information
1abhishekpandey authored Dec 19, 2022
1 parent 97ca4ca commit bce908f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.rudderstack.sdk.java.analytics;

import okhttp3.*;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;

import java.io.IOException;

/**
* This interceptor compresses the HTTP request body. Copied from
* https://github.com/square/okhttp/wiki/Interceptors#rewriting-requests
*/
final class GzipRequestInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}

Request compressedRequest =
originalRequest
.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), gzip(originalRequest.body()))
.build();
return chain.proceed(compressedRequest);
}

private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}

@Override
public long contentLength() {
return -1; // We don't know the compressed length in advance!
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ private static Platform findPlatform() {
return new Platform();
}

OkHttpClient defaultClient() {
OkHttpClient client =
OkHttpClient defaultClient(boolean enableGZIP) {
OkHttpClient.Builder builder =
new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
.writeTimeout(15, TimeUnit.SECONDS);

if (enableGZIP) {
builder.addInterceptor(new GzipRequestInterceptor());
}
OkHttpClient client = builder.build();
return client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static class Builder {
private List<Callback> callbacks;
private int queueCapacity;
private boolean forceTlsV1 = false;
private boolean gzip = true;

Builder(String writeKey) {
if (writeKey == null || writeKey.trim().length() == 0) {
Expand All @@ -165,6 +166,12 @@ public Builder client(OkHttpClient client) {
return this;
}

/** Disable the GZIP client. */
public Builder setGZIP(boolean gzip) {
this.gzip = gzip;
return this;
}

/** Configure debug logging mechanism. By default, nothing is logged. */
public Builder log(Log log) {
if (log == null) {
Expand Down Expand Up @@ -358,7 +365,7 @@ public RudderAnalytics build() {
}

if (client == null) {
client = Platform.get().defaultClient();
client = Platform.get().defaultClient(this.gzip);
}

if (log == null) {
Expand Down

0 comments on commit bce908f

Please sign in to comment.