]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Backend QPS limit refactoring
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 25 Jul 2025 14:19:43 +0000 (16:19 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 28 Jul 2025 09:31:21 +0000 (11:31 +0200)
Use `std::optional` to signal that the QPS limit is not set,
instead of a relying of a special case version of the `QPSLimiter`
that does pretty much nothing.

Signed-off-by: Remi Gacogne <remi.gacogne@powerdns.com>
pdns/dnsdistdist/dnsdist-backend.cc
pdns/dnsdistdist/dnsdist-lbpolicies.cc
pdns/dnsdistdist/dnsdist-lua-bindings.cc
pdns/dnsdistdist/dnsdist-lua.cc
pdns/dnsdistdist/dnsdist-snmp.cc
pdns/dnsdistdist/dnsdist-web.cc
pdns/dnsdistdist/dnsdist.hh

index c93b9e561b33256ce58104c2f6f9e5633cfe6cd5..47593785d346db6a211aea0fcb2fffd9edd16988 100644 (file)
@@ -296,7 +296,7 @@ DownstreamState::DownstreamState(DownstreamState::Config&& config, std::shared_p
   threadStarted.clear();
 
   if (d_config.d_qpsLimit > 0) {
-    qps = QPSLimiter(d_config.d_qpsLimit, d_config.d_qpsLimit);
+    d_qpsLimiter = QPSLimiter(d_config.d_qpsLimit, d_config.d_qpsLimit);
   }
 
   if (d_config.id) {
@@ -1010,6 +1010,11 @@ bool DownstreamState::parseAvailabilityConfigFromStr(DownstreamState::Config& co
   return false;
 }
 
+unsigned int DownstreamState::getQPSLimit() const
+{
+  return d_qpsLimiter ? d_qpsLimiter->getRate() : 0U;
+}
+
 size_t ServerPool::countServers(bool upOnly)
 {
   std::shared_ptr<const ServerPolicy::NumberedServerVector> servers = nullptr;
index ac17eb5efab1ba558230e3a1c350c4e2b7ce5e74..c8749196fef3e84597a1a255caf3fde91ce75347 100644 (file)
@@ -75,7 +75,7 @@ shared_ptr<DownstreamState> leastOutstanding(const ServerPolicy::NumberedServerV
 shared_ptr<DownstreamState> firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq)
 {
   for (auto& d : servers) {
-    if (d.second->isUp() && d.second->qps.checkOnly()) {
+    if (d.second->isUp() && (!d.second->d_qpsLimiter || d.second->d_qpsLimiter->checkOnly())) {
       return d.second;
     }
   }
index 7f019e7528725af86f4bb28c6bb749e669118d92..7d7829aec21a331c663c27c8b8cf5325359c3240 100644 (file)
@@ -112,7 +112,12 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
   /* DownstreamState */
   luaCtx.registerFunction<void (std::shared_ptr<DownstreamState>::*)(int)>("setQPS", [](std::shared_ptr<DownstreamState>& state, int lim) {
     if (state) {
-      state->qps = lim > 0 ? QPSLimiter(lim, lim) : QPSLimiter();
+      if (lim > 0) {
+        state->d_qpsLimiter = QPSLimiter(lim, lim);
+      }
+      else {
+        state->d_qpsLimiter.reset();
+      }
     }
   });
   luaCtx.registerFunction<void (std::shared_ptr<DownstreamState>::*)(string)>("addPool", [](const std::shared_ptr<DownstreamState>& state, const string& pool) {
index f330c2dae7c420f21b5c46b168cb027c9f7c9c83..212f524136faf05880b965d323137ab21a254562 100644 (file)
@@ -967,10 +967,10 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
         const std::string latency = (backend->latencyUsec == 0.0 ? "-" : boost::str(latFmt % (backend->latencyUsec / 1000.0)));
         const std::string latencytcp = (backend->latencyUsecTCP == 0.0 ? "-" : boost::str(latFmt % (backend->latencyUsecTCP / 1000.0)));
         if (showUUIDs) {
-          ret << (fmt % counter % backend->getName() % backend->d_config.remote.toStringWithPort() % status % backend->queryLoad % backend->qps.getRate() % backend->d_config.order % backend->d_config.d_weight % backend->queries.load() % backend->reuseds.load() % (backend->dropRate) % latency % backend->outstanding.load() % pools % *backend->d_config.id % latencytcp) << endl;
+          ret << (fmt % counter % backend->getName() % backend->d_config.remote.toStringWithPort() % status % backend->queryLoad % backend->getQPSLimit() % backend->d_config.order % backend->d_config.d_weight % backend->queries.load() % backend->reuseds.load() % (backend->dropRate) % latency % backend->outstanding.load() % pools % *backend->d_config.id % latencytcp) << endl;
         }
         else {
-          ret << (fmt % counter % backend->getName() % backend->d_config.remote.toStringWithPort() % status % backend->queryLoad % backend->qps.getRate() % backend->d_config.order % backend->d_config.d_weight % backend->queries.load() % backend->reuseds.load() % (backend->dropRate) % latency % backend->outstanding.load() % pools % latencytcp) << endl;
+          ret << (fmt % counter % backend->getName() % backend->d_config.remote.toStringWithPort() % status % backend->queryLoad % backend->getQPSLimit() % backend->d_config.order % backend->d_config.d_weight % backend->queries.load() % backend->reuseds.load() % (backend->dropRate) % latency % backend->outstanding.load() % pools % latencytcp) << endl;
         }
         totQPS += static_cast<uint64_t>(backend->queryLoad);
         totQueries += backend->queries.load();
index 9d4a05c1f051eca557bf07974b9a9f345ee1d3e3..9064d29d71f1c313d6330e2e3c6bea92a2c88c37 100644 (file)
@@ -337,7 +337,7 @@ static int backendStatTable_handler(netsnmp_mib_handler* handler,
         break;
       case COLUMN_BACKENDQPSLIMIT:
         DNSDistSNMPAgent::setCounter64Value(request,
-                                            server->qps.getRate());
+                                            server->getQPSLimit());
         break;
       case COLUMN_BACKENDREUSED:
         DNSDistSNMPAgent::setCounter64Value(request, server->reuseds.load());
index de657f3ee0667ef2eb7d77e87da96fd1a8deec09..061d36e97d95354ab9929a0c007c404c030a1543 100644 (file)
@@ -1092,7 +1092,7 @@ static void addServerToJSON(Json::array& servers, int identifier, const std::sha
     {"state", status},
     {"protocol", backend->getProtocol().toPrettyString()},
     {"qps", (double)backend->queryLoad},
-    {"qpsLimit", (double)backend->qps.getRate()},
+    {"qpsLimit", static_cast<double>(backend->getQPSLimit())},
     {"outstanding", (double)backend->outstanding},
     {"reuseds", (double)backend->reuseds},
     {"weight", (double)backend->d_config.d_weight},
index 3f21e76fec3343d652669ebc57630902ff063b87..133186d1b11d7ad8202bda9aef5b60624dc71a59 100644 (file)
@@ -722,7 +722,7 @@ public:
   std::shared_ptr<TLSCtx> d_tlsCtx{nullptr};
   std::vector<int> sockets;
   StopWatch sw;
-  QPSLimiter qps;
+  std::optional<QPSLimiter> d_qpsLimiter;
 #ifdef HAVE_XSK
   std::vector<std::shared_ptr<XskWorker>> d_xskInfos;
   std::vector<std::shared_ptr<XskSocket>> d_xskSockets;
@@ -874,7 +874,9 @@ public:
   void incQueriesCount()
   {
     ++queries;
-    qps.addHit();
+    if (d_qpsLimiter) {
+      d_qpsLimiter->addHit();
+    }
   }
 
   void incCurrentConnectionsCount();
@@ -932,6 +934,8 @@ public:
     }
     return latencyUsec;
   }
+
+  unsigned int getQPSLimit() const;
 };
 
 void responderThread(std::shared_ptr<DownstreamState> dss);