Skip to content

Commit 804735b

Browse files
authored
Merge pull request #478 from bonitoo-io/issue_445
fix issue #445 : #445
2 parents 3e24fa4 + 96ffd8c commit 804735b

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ public InfluxDBImpl(final String url, final String username, final String passwo
123123
setLogLevel(LOG_LEVEL);
124124

125125
this.gzipRequestInterceptor = new GzipRequestInterceptor();
126-
client.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor);
126+
OkHttpClient.Builder clonedBuilder = client.build().newBuilder();
127+
clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor);
127128

128129
Factory converterFactory = null;
129130
switch (responseFormat) {
130131
case MSGPACK:
131-
client.addInterceptor(chain -> {
132+
clonedBuilder.addInterceptor(chain -> {
132133
Request request = chain.request().newBuilder().addHeader("Accept", APPLICATION_MSGPACK)
133134
.addHeader("Accept-Encoding", "identity").build();
134135
return chain.proceed(request);
@@ -147,8 +148,8 @@ public InfluxDBImpl(final String url, final String username, final String passwo
147148
break;
148149
}
149150

150-
this.retrofit = new Retrofit.Builder().baseUrl(url).client(client.build()).addConverterFactory(converterFactory)
151-
.build();
151+
this.retrofit = new Retrofit.Builder().baseUrl(url).client(
152+
clonedBuilder.build()).addConverterFactory(converterFactory).build();
152153
this.influxDBService = this.retrofit.create(InfluxDBService.class);
153154

154155
}
@@ -171,8 +172,9 @@ public InfluxDBImpl(final String url, final String username, final String passwo
171172
setLogLevel(LOG_LEVEL);
172173

173174
this.gzipRequestInterceptor = new GzipRequestInterceptor();
175+
OkHttpClient.Builder clonedBuilder = client.build().newBuilder();
174176
this.retrofit = new Retrofit.Builder().baseUrl(url)
175-
.client(client.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
177+
.client(clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
176178
.addConverterFactory(MoshiConverterFactory.create()).build();
177179
this.influxDBService = influxDBService;
178180

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.junit.platform.runner.JUnitPlatform;
1919
import org.junit.runner.RunWith;
2020

21+
import okhttp3.OkHttpClient;
22+
2123
import java.io.IOException;
2224
import java.time.Instant;
2325
import java.time.ZoneId;
@@ -27,7 +29,9 @@
2729
import java.util.List;
2830
import java.util.Set;
2931
import java.util.concurrent.BlockingQueue;
32+
import java.util.concurrent.Callable;
3033
import java.util.concurrent.CountDownLatch;
34+
import java.util.concurrent.ExecutorService;
3135
import java.util.concurrent.Executors;
3236
import java.util.concurrent.LinkedBlockingQueue;
3337
import java.util.concurrent.ThreadFactory;
@@ -891,5 +895,62 @@ public void testMessagePackOnOldDbVersion() {
891895
influxDB.describeDatabases();
892896
});
893897
}
894-
898+
899+
/**
900+
* test for issue #445
901+
* make sure reusing of OkHttpClient.Builder causes no error
902+
* @throws InterruptedException
903+
*/
904+
@Test
905+
public void testIssue445() throws InterruptedException {
906+
ExecutorService executor = Executors.newFixedThreadPool(100);
907+
908+
final int maxCallables = 10_000;
909+
List<Callable<String>> callableList = new ArrayList<>(maxCallables);
910+
for (int i = 0; i < maxCallables; i++) {
911+
callableList.add(new Callable<String>() {
912+
@Override
913+
public String call() throws Exception {
914+
MyInfluxDBBean myBean = new MyInfluxDBBean();
915+
return myBean.connectAndDoNothing1();
916+
}
917+
});
918+
}
919+
executor.invokeAll(callableList);
920+
executor.shutdown();
921+
if (!executor.awaitTermination(20, TimeUnit.SECONDS)) {
922+
executor.shutdownNow();
923+
}
924+
Assertions.assertTrue(MyInfluxDBBean.OK);
925+
//assert that MyInfluxDBBean.OKHTTP_BUILDER stays untouched (no interceptor added)
926+
Assertions.assertTrue(MyInfluxDBBean.OKHTTP_BUILDER.interceptors().isEmpty());
927+
}
928+
929+
private static final class MyInfluxDBBean {
930+
931+
static final OkHttpClient.Builder OKHTTP_BUILDER = new OkHttpClient.Builder();
932+
static Boolean OK = true;
933+
static final String URL = "http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true);
934+
935+
InfluxDB influxClient;
936+
937+
String connectAndDoNothing1() {
938+
synchronized (OK) {
939+
if (!OK) {
940+
return null;
941+
}
942+
}
943+
try {
944+
influxClient = InfluxDBFactory.connect(URL, "admin", "admin", OKHTTP_BUILDER);
945+
influxClient.close();
946+
} catch (Exception e) {
947+
synchronized (OK) {
948+
if (OK) {
949+
OK = false;
950+
}
951+
}
952+
}
953+
return null;
954+
}
955+
}
895956
}

0 commit comments

Comments
 (0)