Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class ClickHouseConfig implements Serializable {
static final class ClientOptions {
private static final ClientOptions instance = new ClientOptions();
private static final ClientOptions INSTANCE = new ClientOptions();

private final Map<String, ClickHouseOption> customOptions;

Expand Down Expand Up @@ -138,7 +138,7 @@ protected static final Object mergeMetricRegistry(List<ClickHouseConfig> list) {
public static Map<ClickHouseOption, Serializable> toClientOptions(Map<?, ?> props) {
Map<ClickHouseOption, Serializable> options = new HashMap<>();
if (props != null && !props.isEmpty()) {
Map<String, ClickHouseOption> customOptions = ClientOptions.instance.customOptions;
Map<String, ClickHouseOption> customOptions = ClientOptions.INSTANCE.customOptions;
for (Entry<?, ?> e : props.entrySet()) {
if (e.getKey() == null || e.getValue() == null) {
continue;
Expand Down Expand Up @@ -202,6 +202,7 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Map<?, ?> prop
private final String sslKey;
private final boolean useBlockingQueue;
private final boolean useObjectsInArray;
private final boolean useNoProxy;
private final boolean useServerTimeZone;
private final boolean useServerTimeZoneForDates;
private final TimeZone timeZoneForDate;
Expand Down Expand Up @@ -297,6 +298,7 @@ public ClickHouseConfig(Map<ClickHouseOption, Serializable> options, ClickHouseC
this.sslKey = (String) getOption(ClickHouseClientOption.SSL_KEY);
this.useBlockingQueue = (boolean) getOption(ClickHouseClientOption.USE_BLOCKING_QUEUE);
this.useObjectsInArray = (boolean) getOption(ClickHouseClientOption.USE_OBJECTS_IN_ARRAYS);
this.useNoProxy = (boolean) getOption(ClickHouseClientOption.USE_NO_PROXY);
this.useServerTimeZone = (boolean) getOption(ClickHouseClientOption.USE_SERVER_TIME_ZONE);
this.useServerTimeZoneForDates = (boolean) getOption(ClickHouseClientOption.USE_SERVER_TIME_ZONE_FOR_DATES);

Expand Down Expand Up @@ -649,6 +651,10 @@ public boolean isUseObjectsInArray() {
return useObjectsInArray;
}

public boolean isUseNoProxy() {
return useNoProxy;
}

public boolean isUseServerTimeZone() {
return useServerTimeZone;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ public enum ClickHouseClientOption implements ClickHouseOption {
*/
USE_OBJECTS_IN_ARRAYS("use_objects_in_arrays", false,
"Whether Object[] should be used instead of primitive arrays."),
/**
* Whether to access ClickHouse server directly without using system wide proxy
* including the one defined in JVM system properties.
*/
USE_NO_PROXY("use_no_proxy", false,
"Whether to access ClickHouse server directly without using system wide proxy including the one defined in JVM system properties."),
/**
* Whether to use server time zone.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.clickhouse.client.grpc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -11,6 +13,8 @@
import com.google.gson.stream.JsonReader;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ProxiedSocketAddress;
import io.grpc.ProxyDetector;
import io.grpc.Status;
import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseConfig;
Expand All @@ -22,6 +26,18 @@
import com.clickhouse.client.logging.LoggerFactory;

public abstract class ClickHouseGrpcChannelFactory {
static class NoProxyDetector implements ProxyDetector {
static final NoProxyDetector INSTANCE = new NoProxyDetector();

private NoProxyDetector() {
}

@Override
public ProxiedSocketAddress proxyFor(SocketAddress arg0) throws IOException {
return null;
}
}

private static final Logger log = LoggerFactory.getLogger(ClickHouseGrpcChannelFactory.class);

private static final String PROP_NAME = "name";
Expand Down Expand Up @@ -164,6 +180,9 @@ protected void setupMisc() {
builder.enableFullStreamDecompression();
}

if (config.isUseNoProxy()) {
builder.proxyDetector(NoProxyDetector.INSTANCE);
}
// TODO add interceptor to customize retry
builder.maxInboundMessageSize((int) config.getOption(ClickHouseGrpcOption.MAX_INBOUND_MESSAGE_SIZE))
.maxInboundMetadataSize((int) config.getOption(ClickHouseGrpcOption.MAX_INBOUND_METADATA_SIZE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.UncheckedIOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -107,7 +108,9 @@ private ClickHouseHttpResponse buildResponse() throws IOException {
}

private HttpURLConnection newConnection(String url, boolean post) throws IOException {
HttpURLConnection newConn = (HttpURLConnection) new URL(url).openConnection();
HttpURLConnection newConn = config.isUseNoProxy()
? (HttpURLConnection) new URL(url).openConnection(Proxy.NO_PROXY)
: (HttpURLConnection) new URL(url).openConnection();

if ((newConn instanceof HttpsURLConnection) && config.isSsl()) {
HttpsURLConnection secureConn = (HttpsURLConnection) newConn;
Expand Down Expand Up @@ -143,12 +146,10 @@ private String getResponseHeader(String header, String defaultValue) {
private void setHeaders(HttpURLConnection conn, Map<String, String> headers) {
headers = mergeHeaders(headers);

if (headers == null || headers.isEmpty()) {
return;
}

for (Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
if (headers != null && !headers.isEmpty()) {
for (Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.io.UncheckedIOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpConnectTimeoutException;
Expand All @@ -52,6 +55,25 @@
import javax.net.ssl.SSLContext;

public class HttpClientConnectionImpl extends ClickHouseHttpConnection {
static class NoProxySelector extends ProxySelector {
static final NoProxySelector INSTANCE = new NoProxySelector();

private static final List<Proxy> NO_PROXY_LIST = List.of(Proxy.NO_PROXY);

private NoProxySelector() {
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException e) {
// ignore
}

@Override
public List<Proxy> select(URI uri) {
return NO_PROXY_LIST;
}
}

private static final Logger log = LoggerFactory.getLogger(HttpClientConnectionImpl.class);

private final HttpClient httpClient;
Expand Down Expand Up @@ -147,6 +169,9 @@ protected HttpClientConnectionImpl(ClickHouseNode server, ClickHouseRequest<?> r
if (executor != null) {
builder.executor(executor);
}
if (config.isUseNoProxy()) {
builder.proxy(NoProxySelector.INSTANCE);
}
if (config.isSsl()) {
builder.sslContext(ClickHouseSslContextProvider.getProvider().getSslContext(SSLContext.class, config)
.orElse(null));
Expand Down