Skip to content

Commit 99dbf49

Browse files
authored
fix: use gzip compression only for /write endpoint (#851)
1 parent 8036250 commit 99dbf49

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
### Improvements
66
- Add implementation information to `Jar` manifest [PR #847](https://github.com/influxdata/influxdb-java/pull/847)
77

8+
### Fixes
9+
- Only the request to /write endpoint should be compressed by GZIP [PR #851](https://github.com/influxdata/influxdb-java/pull/851)
10+
811
## 2.22 [2021-09-17]
912

1013
### Improvements

src/main/java/org/influxdb/impl/GzipRequestInterceptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.concurrent.atomic.AtomicBoolean;
5+
import java.util.regex.Pattern;
56

67
import okhttp3.Interceptor;
78
import okhttp3.MediaType;
@@ -19,6 +20,8 @@
1920
*/
2021
final class GzipRequestInterceptor implements Interceptor {
2122

23+
private static final Pattern WRITE_PATTERN = Pattern.compile(".*/write", Pattern.CASE_INSENSITIVE);
24+
2225
private AtomicBoolean enabled = new AtomicBoolean(false);
2326

2427
GzipRequestInterceptor() {
@@ -48,6 +51,10 @@ public Response intercept(final Interceptor.Chain chain) throws IOException {
4851
return chain.proceed(originalRequest);
4952
}
5053

54+
if (!WRITE_PATTERN.matcher(originalRequest.url().encodedPath()).matches()) {
55+
return chain.proceed(originalRequest);
56+
}
57+
5158
Request compressedRequest = originalRequest.newBuilder().header("Content-Encoding", "gzip")
5259
.method(originalRequest.method(), gzip(body)).build();
5360
return chain.proceed(compressedRequest);

src/test/java/org/influxdb/InfluxDBTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.util.function.Consumer;
4242
import java.util.regex.Pattern;
4343

44+
import static org.assertj.core.api.Assertions.assertThat;
45+
4446
/**
4547
* Test the InfluxDB API.
4648
*
@@ -1457,6 +1459,18 @@ public String call() throws Exception {
14571459
Assertions.assertTrue(MyInfluxDBBean.OKHTTP_BUILDER.interceptors().isEmpty());
14581460
}
14591461

1462+
@Test
1463+
public void testQueryPostWithGZIPCompression() {
1464+
this.influxDB.enableGzip();
1465+
String database = "db_gzip_" + System.currentTimeMillis();
1466+
this.influxDB.query(new Query(String.format("CREATE DATABASE %s", database), null, true));
1467+
QueryResult query = this.influxDB.query(new Query("SHOW DATABASES", null, true));
1468+
assertThat(query.getResults()).hasSize(1);
1469+
assertThat(query.getResults().get(0).getSeries()).hasSize(1);
1470+
assertThat(query.getResults().get(0).getSeries().get(0).getValues()).contains(Collections.singletonList(database));
1471+
this.influxDB.query(new Query(String.format("DROP DATABASE %s", database), null, true));
1472+
}
1473+
14601474
private static final class MyInfluxDBBean {
14611475

14621476
static final OkHttpClient.Builder OKHTTP_BUILDER = new OkHttpClient.Builder();

0 commit comments

Comments
 (0)