Skip to content

Commit 5995a7e

Browse files
authored
Merge pull request #2084 from ClickHouse/fix-default-timeouts
[client-v2] Removed old defaults and replaced new ones
2 parents f01a30e + 52c7631 commit 5995a7e

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -255,47 +255,6 @@ public static class Builder {
255255
public Builder() {
256256
this.endpoints = new HashSet<>();
257257
this.configuration = new HashMap<String, String>();
258-
// TODO: set defaults configuration values
259-
this.setConnectTimeout(30, SECONDS)
260-
.setSocketTimeout(2, SECONDS)
261-
.setSocketRcvbuf(804800)
262-
.setSocketSndbuf(804800)
263-
.compressServerResponse(true)
264-
.compressClientRequest(false);
265-
}
266-
267-
/**
268-
* Builds a client object with the provided configuration through URL parameters.
269-
* (e.g. http://localhost:8123/someDatabase?user=default)
270-
* @param urlString - URL formatted string with protocol, host, port, and client configuration settings.
271-
* @return Client - a client object
272-
*/
273-
public Builder fromUrl(String urlString) {
274-
try {
275-
URL url = new URL(urlString);
276-
Map<String, String> urlProperties = HttpAPIClientHelper.parseUrlParameters(url);
277-
278-
// Add the endpoint
279-
boolean secure = url.getProtocol().equalsIgnoreCase("https");
280-
int port = url.getPort();
281-
if (port == -1) {
282-
port = secure ? ClickHouseHttpProto.DEFAULT_HTTPS_PORT : ClickHouseHttpProto.DEFAULT_HTTP_PORT;
283-
}
284-
this.addEndpoint(Protocol.HTTP, url.getHost(), port, secure);
285-
286-
for (ClientConfigProperties properties: ClientConfigProperties.values()) {
287-
String value = urlProperties.get(properties.getKey());
288-
if (value != null) {
289-
this.configuration.put(properties.getKey(), value);
290-
}
291-
}
292-
293-
// Set the database
294-
} catch (java.net.MalformedURLException e) {
295-
throw new IllegalArgumentException("Configuration via URL should be done with a valid URL string", e);
296-
}
297-
298-
return this;
299258
}
300259

301260
/**
@@ -520,6 +479,7 @@ public Builder setSocketTimeout(long timeout) {
520479

521480
/**
522481
* Default socket timeout in milliseconds. Timeout is applied to read and write operations.
482+
* Default is 0.
523483
*
524484
* @param timeout - socket timeout value
525485
* @param unit - time unit
@@ -1076,6 +1036,7 @@ public Client build() {
10761036
* Default size for a buffers used in networking.
10771037
*/
10781038
public static final int DEFAULT_BUFFER_SIZE = 8192;
1039+
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 804800;
10791040

10801041
private void setDefaults() {
10811042

@@ -1169,6 +1130,18 @@ private void setDefaults() {
11691130
if (!configuration.containsKey(ClientConfigProperties.APP_COMPRESSED_DATA.getKey())) {
11701131
appCompressedData(false);
11711132
}
1133+
1134+
if (!configuration.containsKey(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey())) {
1135+
setSocketTimeout(0, SECONDS);
1136+
}
1137+
1138+
if (!configuration.containsKey(ClientConfigProperties.SOCKET_RCVBUF_OPT.getKey())) {
1139+
setSocketRcvbuf(DEFAULT_SOCKET_BUFFER_SIZE);
1140+
}
1141+
1142+
if (!configuration.containsKey(ClientConfigProperties.SOCKET_SNDBUF_OPT.getKey())) {
1143+
setSocketSndbuf(DEFAULT_SOCKET_BUFFER_SIZE);
1144+
}
11721145
}
11731146
}
11741147

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import java.io.ByteArrayInputStream;
4141
import java.net.Socket;
42+
import java.net.SocketTimeoutException;
4243
import java.nio.ByteBuffer;
4344
import java.nio.charset.StandardCharsets;
4445
import java.time.temporal.ChronoUnit;
@@ -52,6 +53,7 @@
5253
import java.util.concurrent.CompletableFuture;
5354
import java.util.concurrent.ExecutionException;
5455
import java.util.concurrent.TimeUnit;
56+
import java.util.concurrent.TimeoutException;
5557
import java.util.concurrent.atomic.AtomicInteger;
5658
import java.util.regex.Pattern;
5759
import java.util.function.Supplier;
@@ -1025,6 +1027,38 @@ public void testJWTWithCloud() throws Exception {
10251027
}
10261028
}
10271029

1030+
@Test(groups = { "integration" })
1031+
public void testWithDefaultTimeouts() {
1032+
if (isCloud()) {
1033+
return; // mocked server
1034+
}
1035+
1036+
int proxyPort = new Random().nextInt(1000) + 10000;
1037+
WireMockServer proxy = new WireMockServer(WireMockConfiguration
1038+
.options().port(proxyPort)
1039+
.notifier(new Slf4jNotifier(true)));
1040+
proxy.start();
1041+
proxy.addStubMapping(WireMock.post(WireMock.anyUrl())
1042+
.willReturn(WireMock.aResponse().withFixedDelay(5000)
1043+
.withStatus(HttpStatus.SC_OK)
1044+
.withHeader("X-ClickHouse-Summary", "{ \"read_bytes\": \"10\", \"read_rows\": \"1\"}")).build());
1045+
1046+
try (Client client = new Client.Builder().addEndpoint(Protocol.HTTP, "localhost", proxyPort, false)
1047+
.setUsername("default")
1048+
.setPassword("")
1049+
.useNewImplementation(true)
1050+
.build()) {
1051+
int startTime = (int) System.currentTimeMillis();
1052+
try {
1053+
client.query("SELECT 1").get();
1054+
} catch (Exception e) {
1055+
Assert.fail("Elapsed Time: " + (System.currentTimeMillis() - startTime), e);
1056+
}
1057+
} finally {
1058+
proxy.stop();
1059+
}
1060+
}
1061+
10281062
protected Client.Builder newClient() {
10291063
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
10301064
boolean isSecure = isCloud();

0 commit comments

Comments
 (0)