From 8c9c4215d1a168d26d8eb7a14dacc4c1a3bdabea Mon Sep 17 00:00:00 2001 From: Oraoto Date: Wed, 30 Sep 2020 15:12:52 +0800 Subject: [PATCH 1/2] Add TCP_NODELAY support --- clickhouse/base/socket.cpp | 5 +++++ clickhouse/base/socket.h | 3 +++ clickhouse/client.cpp | 3 +++ clickhouse/client.h | 3 +++ 4 files changed, 14 insertions(+) diff --git a/clickhouse/base/socket.cpp b/clickhouse/base/socket.cpp index 8e362c6a..4ed45252 100644 --- a/clickhouse/base/socket.cpp +++ b/clickhouse/base/socket.cpp @@ -169,6 +169,11 @@ void SocketHolder::SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept { #endif } +void SocketHolder::SetTcpNoDelay(bool nodelay) noexcept { + int val = nodelay; + setsockopt(handle_, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); +} + SocketHolder& SocketHolder::operator = (SocketHolder&& other) noexcept { if (this != &other) { Close(); diff --git a/clickhouse/base/socket.h b/clickhouse/base/socket.h index a38a8290..256608dc 100644 --- a/clickhouse/base/socket.h +++ b/clickhouse/base/socket.h @@ -62,6 +62,9 @@ class SocketHolder { /// before dropping the connection. void SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept; + /// @params nodelay whether to enable TCP_NODELAY + void SetTcpNoDelay(bool nodelay) noexcept; + SocketHolder& operator = (SocketHolder&& other) noexcept; operator SOCKET () const noexcept; diff --git a/clickhouse/client.cpp b/clickhouse/client.cpp index 2c82d2ad..a361509d 100644 --- a/clickhouse/client.cpp +++ b/clickhouse/client.cpp @@ -277,6 +277,9 @@ void Client::Impl::ResetConnection() { options_.tcp_keepalive_intvl.count(), options_.tcp_keepalive_cnt); } + if (options_.tcp_nodelay) { + s.SetTcpNoDelay(options_.tcp_nodelay); + } socket_ = std::move(s); socket_input_ = SocketInput(socket_); diff --git a/clickhouse/client.h b/clickhouse/client.h index a57c33b6..88db6830 100644 --- a/clickhouse/client.h +++ b/clickhouse/client.h @@ -80,6 +80,9 @@ struct ClientOptions { DECLARE_FIELD(tcp_keepalive_intvl, std::chrono::seconds, SetTcpKeepAliveInterval, std::chrono::seconds(5)); DECLARE_FIELD(tcp_keepalive_cnt, unsigned int, SetTcpKeepAliveCount, 3); + // TCP options + DECLARE_FIELD(tcp_nodelay, bool, TcpNoDelay, false); + #undef DECLARE_FIELD }; From eb5971f84daa7728ae8abc72eca77a6705792970 Mon Sep 17 00:00:00 2001 From: Oraoto Date: Wed, 30 Sep 2020 16:35:29 +0800 Subject: [PATCH 2/2] Enable TCP_NODELAY by default --- clickhouse/client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clickhouse/client.h b/clickhouse/client.h index 88db6830..194e0e31 100644 --- a/clickhouse/client.h +++ b/clickhouse/client.h @@ -81,7 +81,7 @@ struct ClientOptions { DECLARE_FIELD(tcp_keepalive_cnt, unsigned int, SetTcpKeepAliveCount, 3); // TCP options - DECLARE_FIELD(tcp_nodelay, bool, TcpNoDelay, false); + DECLARE_FIELD(tcp_nodelay, bool, TcpNoDelay, true); #undef DECLARE_FIELD };