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

fix: use gzip compression only for /write endpoint #851

Merged
merged 3 commits into from
Jul 5, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Improvements
- Add implementation information to `Jar` manifest [PR #847](https://github.com/influxdata/influxdb-java/pull/847)

### Fixes
- Only the request to /write endpoint should be compressed by GZIP [PR #851](https://github.com/influxdata/influxdb-java/pull/851)

## 2.22 [2021-09-17]

### Improvements
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/influxdb/impl/GzipRequestInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

import okhttp3.Interceptor;
import okhttp3.MediaType;
Expand All @@ -19,6 +20,8 @@
*/
final class GzipRequestInterceptor implements Interceptor {

private static final Pattern WRITE_PATTERN = Pattern.compile(".*/write", Pattern.CASE_INSENSITIVE);

private AtomicBoolean enabled = new AtomicBoolean(false);

GzipRequestInterceptor() {
Expand Down Expand Up @@ -48,6 +51,10 @@ public Response intercept(final Interceptor.Chain chain) throws IOException {
return chain.proceed(originalRequest);
}

if (!WRITE_PATTERN.matcher(originalRequest.url().encodedPath()).matches()) {
return chain.proceed(originalRequest);
}

Request compressedRequest = originalRequest.newBuilder().header("Content-Encoding", "gzip")
.method(originalRequest.method(), gzip(body)).build();
return chain.proceed(compressedRequest);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.function.Consumer;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test the InfluxDB API.
*
Expand Down Expand Up @@ -1457,6 +1459,18 @@ public String call() throws Exception {
Assertions.assertTrue(MyInfluxDBBean.OKHTTP_BUILDER.interceptors().isEmpty());
}

@Test
public void testQueryPostWithGZIPCompression() {
this.influxDB.enableGzip();
String database = "db_gzip_" + System.currentTimeMillis();
this.influxDB.query(new Query(String.format("CREATE DATABASE %s", database), null, true));
QueryResult query = this.influxDB.query(new Query("SHOW DATABASES", null, true));
assertThat(query.getResults()).hasSize(1);
assertThat(query.getResults().get(0).getSeries()).hasSize(1);
assertThat(query.getResults().get(0).getSeries().get(0).getValues()).contains(Collections.singletonList(database));
this.influxDB.query(new Query(String.format("DROP DATABASE %s", database), null, true));
}

private static final class MyInfluxDBBean {

static final OkHttpClient.Builder OKHTTP_BUILDER = new OkHttpClient.Builder();
Expand Down