]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add setMaxCachedTCPConnectionsPerDownstream()
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 22 Mar 2021 17:45:01 +0000 (18:45 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 26 Mar 2021 09:52:34 +0000 (10:52 +0100)
pdns/dnsdist-console.cc
pdns/dnsdist-lua.cc
pdns/dnsdist-tcp.cc
pdns/dnsdist.hh
pdns/dnsdistdist/docs/reference/tuning.rst

index f4dfd2c8d67dd460b4305fd39b3bae35d21674f7..c3bd5e20c4e0318e881bbe25cb9ff27dccb05c3f 100644 (file)
@@ -586,6 +586,7 @@ const std::vector<ConsoleKeyword> 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" },
index de3fbe701b3c579b0a038a99b86e1ee2b5d54e2e..f234741d80b9652d2d3798a429c9bd9a34d377cb 100644 (file)
@@ -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; });
index 19185d9d53c3d8e4d8207b878a3c42986674e9f2..c52015021b29ae1cf203470080ef67975365438b 100644 (file)
@@ -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::shared_ptr<DownstreamState>, std::deque<std::shared_ptr<TCPConnectionToBackend>>> 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::shared_ptr<DownstreamState>, std::deque<std::shared_ptr<TCPConnectionToBackend>>> DownstreamConnectionsManager::t_downstreamConnections;
-const size_t DownstreamConnectionsManager::s_maxCachedConnectionsPerDownstream{20};
+size_t DownstreamConnectionsManager::s_maxCachedConnectionsPerDownstream{10};
 
 static void decrementTCPClientCount(const ComboAddress& client)
 {
index 434647a3dfa245c18edce6af20927416345f345d..a9ecda0a06df70a95ce36f307b456e41de18ffda 100644 (file)
@@ -1229,6 +1229,8 @@ struct LocalHolders
 vector<std::function<void(void)>> 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 */
index 49fccdcb6806beddfa378881f80453054aee4c9e..e483bf2990deaeef9bf8c7ca57a88c8ada6e7883 100644 (file)
@@ -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