From: Remi Gacogne Date: Mon, 22 Mar 2021 17:45:01 +0000 (+0100) Subject: dnsdist: Add setMaxCachedTCPConnectionsPerDownstream() X-Git-Tag: rec-4.6.0-alpha0~1^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6d26db15c28a56b149d2a18f24ca239127e62bf3;p=thirdparty%2Fpdns.git dnsdist: Add setMaxCachedTCPConnectionsPerDownstream() --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index f4dfd2c8d6..c3bd5e20c4 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -586,6 +586,7 @@ const std::vector g_consoleKeywords{ { "setECSSourcePrefixV6", true, "prefix-length", "the EDNS Client Subnet prefix-length used for IPv6 queries" }, { "setKey", true, "key", "set access key to that key" }, { "setLocal", true, "addr [, {doTCP=true, reusePort=false, tcpFastOpenQueueSize=0, interface=\"\", cpus={}}]", "reset the list of addresses we listen on to this address" }, + { "setMaxCachedTCPConnectionsPerDownstream", true, "max", "Set the maximum number of inactive TCP connections to a backend cached by each worker TCP thread" }, { "setMaxTCPClientThreads", true, "n", "set the maximum of TCP client threads, handling TCP connections" }, { "setMaxTCPConnectionDuration", true, "n", "set the maximum duration of an incoming TCP connection, in seconds. 0 means unlimited" }, { "setMaxTCPConnectionsPerClient", true, "n", "set the maximum number of TCP connections per client. 0 means unlimited" }, diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index de3fbe701b..f234741d80 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -1188,6 +1188,10 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); + luaCtx.writeFunction("setMaxCachedTCPConnectionsPerDownstream", [](size_t max) { + setMaxCachedTCPConnectionsPerDownstream(max); + }); + luaCtx.writeFunction("setCacheCleaningDelay", [](uint32_t delay) { g_cacheCleaningDelay = delay; }); luaCtx.writeFunction("setCacheCleaningPercentage", [](uint16_t percentage) { if (percentage < 100) g_cacheCleaningPercentage = percentage; else g_cacheCleaningPercentage = 100; }); diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 19185d9d53..c52015021b 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -127,13 +127,12 @@ public: const auto& ds = conn->getDS(); auto& list = t_downstreamConnections[ds]; - if (list.size() >= s_maxCachedConnectionsPerDownstream) { + while (list.size() >= s_maxCachedConnectionsPerDownstream) { /* too many connections queued already */ - conn.reset(); - } - else { - list.push_back(std::move(conn)); + list.pop_front(); } + + list.push_back(std::move(conn)); } static void cleanupClosedTCPConnections(struct timeval now) @@ -183,13 +182,23 @@ public: return count; } + static void setMaxCachedConnectionsPerDownstream(size_t max) + { + s_maxCachedConnectionsPerDownstream = max; + } + private: static thread_local map, std::deque>> t_downstreamConnections; - static const size_t s_maxCachedConnectionsPerDownstream; + static size_t s_maxCachedConnectionsPerDownstream; }; +void setMaxCachedTCPConnectionsPerDownstream(size_t max) +{ + DownstreamConnectionsManager::setMaxCachedConnectionsPerDownstream(max); +} + thread_local map, std::deque>> DownstreamConnectionsManager::t_downstreamConnections; -const size_t DownstreamConnectionsManager::s_maxCachedConnectionsPerDownstream{20}; +size_t DownstreamConnectionsManager::s_maxCachedConnectionsPerDownstream{10}; static void decrementTCPClientCount(const ComboAddress& client) { diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 434647a3df..a9ecda0a06 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -1229,6 +1229,8 @@ struct LocalHolders vector> setupLua(bool client, const std::string& config); void tcpAcceptorThread(ClientState* p); +void setMaxCachedTCPConnectionsPerDownstream(size_t max); + #ifdef HAVE_DNS_OVER_HTTPS void dohThread(ClientState* cs); #endif /* HAVE_DNS_OVER_HTTPS */ diff --git a/pdns/dnsdistdist/docs/reference/tuning.rst b/pdns/dnsdistdist/docs/reference/tuning.rst index 49fccdcb68..e483bf2990 100644 --- a/pdns/dnsdistdist/docs/reference/tuning.rst +++ b/pdns/dnsdistdist/docs/reference/tuning.rst @@ -1,6 +1,14 @@ Tuning related functions ======================== +.. function:: setMaxCachedTCPConnectionsPerDownstream(max) + + .. versionadded:: 1.6.0 + + Set the maximum number of inactive TCP connections to a backend cached by each TCP worker thread. These connections can be reused when a new query comes in, instead of having to establish a new connection. dnsdist regularly checks whether the other end has closed any cached connection, closing them in that case. + + :param int max: The maximum number of inactive connections to keep. Default is 10, so 10 connections per backend and per TCP worker thread. + .. function:: setMaxTCPClientThreads(num) .. versionchanged:: 1.6.0