Skip to content

Commit 4514191

Browse files
committed
Merge pull request influxdata#123 from mnuessler/timeout
Allow configuration of connection timeout parameters
2 parents 04b15a3 + 8a1fcee commit 4514191

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@
8989
<version>1.10.19</version>
9090
<scope>test</scope>
9191
</dependency>
92+
<dependency>
93+
<groupId>com.github.tomakehurst</groupId>
94+
<artifactId>wiremock</artifactId>
95+
<version>1.58</version>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.slf4j</groupId>
100+
<artifactId>slf4j-simple</artifactId>
101+
<version>1.7.5</version>
102+
<scope>test</scope>
103+
</dependency>
92104
<dependency>
93105
<groupId>com.google.guava</groupId>
94106
<artifactId>guava</artifactId>

src/main/java/org/influxdb/InfluxDB.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.influxdb;
22

3+
import java.net.URLConnection;
34
import java.util.List;
45
import java.util.concurrent.TimeUnit;
56

@@ -22,6 +23,9 @@
2223
*
2324
*/
2425
public interface InfluxDB {
26+
int CONNECT_TIMEOUT_SECONDS_DEFAULT = 15;
27+
int READ_TIMEOUT_SECONDS_DEFAULT = 20;
28+
int WRITE_TIMEOUT_SECONDS_DEFAULT = 20;
2529

2630
/** Controls the level of logging of the REST layer. */
2731
public enum LogLevel {
@@ -171,4 +175,41 @@ public String value() {
171175
*/
172176
public List<String> describeDatabases();
173177

174-
}
178+
/**
179+
* Sets the default connect timeout for new connections. A value of 0 means no timeout. If not
180+
* set explicitly, a default timeout of CONNECT_TIMEOUT_SECONDS_DEFAULT seconds will be used.
181+
*
182+
* @param connectTimeout
183+
* the connect timeout
184+
* @param timeUnit
185+
* the time unit for the connect timeout
186+
*
187+
* @see java.net.URLConnection#setConnectTimeout(int)
188+
*/
189+
void setConnectTimeout(long connectTimeout, TimeUnit timeUnit);
190+
191+
/**
192+
* Sets the default read timeout for new connections. A value of 0 means no timeout. If not
193+
* set explicitly, a default timeout of READ_TIMEOUT_SECONDS_DEFAULT seconds will be used.
194+
*
195+
* @param readTimeout
196+
* the read timeout
197+
* @param timeUnit
198+
* the time unit for the read timeout
199+
*
200+
* @see java.net.URLConnection#setReadTimeout(int)
201+
*/
202+
void setReadTimeout(long readTimeout, TimeUnit timeUnit);
203+
204+
/**
205+
* Sets the default write timeout for new connections. A value of 0 means no timeout. If not
206+
* set explicitly, a default timeout of WRITE_TIMEOUT_SECONDS_DEFAULT seconds will be used.
207+
*
208+
* @param writeTimeout
209+
* the write timeout
210+
* @param timeUnit
211+
* the time unit for the write timeout
212+
*/
213+
void setWriteTimeout(long writeTimeout, TimeUnit timeUnit);
214+
215+
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class InfluxDBImpl implements InfluxDB {
4242
private final AtomicLong unBatchedCount = new AtomicLong();
4343
private final AtomicLong batchedCount = new AtomicLong();
4444
private LogLevel logLevel = LogLevel.NONE;
45+
private OkHttpClient okHttpClient;
4546

4647
/**
4748
* Constructor which should only be used from the InfluxDBFactory.
@@ -57,7 +58,11 @@ public InfluxDBImpl(final String url, final String username, final String passwo
5758
super();
5859
this.username = username;
5960
this.password = password;
60-
Client client = new OkClient(new OkHttpClient());
61+
okHttpClient = new OkHttpClient();
62+
okHttpClient.setConnectTimeout(CONNECT_TIMEOUT_SECONDS_DEFAULT, TimeUnit.SECONDS);
63+
okHttpClient.setReadTimeout(READ_TIMEOUT_SECONDS_DEFAULT, TimeUnit.SECONDS);
64+
okHttpClient.setWriteTimeout(WRITE_TIMEOUT_SECONDS_DEFAULT, TimeUnit.SECONDS);
65+
Client client = new OkClient(okHttpClient);
6166
this.restAdapter = new RestAdapter.Builder()
6267
.setEndpoint(url)
6368
.setErrorHandler(new InfluxDBErrorHandler())
@@ -219,4 +224,28 @@ public List<String> describeDatabases() {
219224
return databases;
220225
}
221226

227+
/**
228+
* {@inheritDoc}
229+
*/
230+
@Override
231+
public void setConnectTimeout(long connectTimeout, TimeUnit timeUnit) {
232+
okHttpClient.setConnectTimeout(connectTimeout, timeUnit);
233+
}
234+
235+
/**
236+
* {@inheritDoc}
237+
*/
238+
@Override
239+
public void setReadTimeout(long readTimeout, TimeUnit timeUnit) {
240+
okHttpClient.setReadTimeout(readTimeout, timeUnit);
241+
}
242+
243+
/**
244+
* {@inheritDoc}
245+
*/
246+
@Override
247+
public void setWriteTimeout(long writeTimeout, TimeUnit timeUnit) {
248+
okHttpClient.setWriteTimeout(writeTimeout, timeUnit);
249+
}
250+
222251
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.influxdb;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
7+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
import org.influxdb.dto.Query;
12+
import org.testng.annotations.AfterClass;
13+
import org.testng.annotations.BeforeClass;
14+
import org.testng.annotations.Test;
15+
16+
import retrofit.RetrofitError;
17+
18+
import com.github.tomakehurst.wiremock.WireMockServer;
19+
import com.github.tomakehurst.wiremock.client.WireMock;
20+
21+
/**
22+
* Test the timeout settings.
23+
*/
24+
public class InfluxDBTimeoutTest {
25+
private static final int WIREMOCK_PORT = 8080;
26+
27+
private WireMockServer wireMockServer;
28+
29+
@BeforeClass
30+
public void setUp() {
31+
wireMockServer = new WireMockServer(wireMockConfig().port(WIREMOCK_PORT));
32+
wireMockServer.start();
33+
WireMock.addRequestProcessingDelay(2 * 1000);
34+
WireMock.configureFor(WIREMOCK_PORT);
35+
36+
stubFor(get(urlPathEqualTo("/query")).willReturn(
37+
aResponse()
38+
.withStatus(200)
39+
.withHeader("Content-Type", "application/json")
40+
.withBody("{}")));
41+
}
42+
43+
@AfterClass
44+
public void tearDown() {
45+
if (wireMockServer != null) {
46+
wireMockServer.stop();
47+
}
48+
}
49+
50+
@Test(timeOut = 3 * 1000, expectedExceptions = RetrofitError.class, expectedExceptionsMessageRegExp = "connect timed out")
51+
public void testConnectTimeout() {
52+
// connect to any non-routable IP address will result in a connect timeout
53+
InfluxDB influxDB = InfluxDBFactory.connect("http://10.0.0.0:" + WIREMOCK_PORT, "user", "password");
54+
influxDB.setConnectTimeout(1, TimeUnit.SECONDS);
55+
56+
influxDB.query(new Query("SELECT value FROM cpu", "test_db"));
57+
}
58+
59+
@Test(timeOut = 3 * 1000, expectedExceptions = RetrofitError.class, expectedExceptionsMessageRegExp = "timeout")
60+
public void testReadTimeout() {
61+
InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:" + WIREMOCK_PORT, "user", "password");
62+
influxDB.setReadTimeout(1, TimeUnit.SECONDS);
63+
64+
influxDB.query(new Query("SELECT value FROM cpu", "test_db"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)