]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Make ServerPolicy::NumberedServerVector const 11852/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 16 Aug 2022 11:35:27 +0000 (13:35 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 16 Aug 2022 11:35:27 +0000 (13:35 +0200)
As suggested by Otto (Thanks!).

pdns/dnsdist-lbpolicies.hh
pdns/dnsdist.hh
pdns/dnsdistdist/dnsdist-backend.cc
pdns/dnsdistdist/dnsdist-lbpolicies.cc

index 395deec2b5e9e00534c9c0445559566790818dea..a1332c77297db2af3edfbc4c589591ddf558d720 100644 (file)
@@ -96,7 +96,7 @@ void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<Serve
 void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
 void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
 
-const std::shared_ptr<ServerPolicy::NumberedServerVector> getDownstreamCandidates(const map<std::string,std::shared_ptr<ServerPool>>& pools, const std::string& poolName);
+const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const map<std::string,std::shared_ptr<ServerPool>>& pools, const std::string& poolName);
 
 std::shared_ptr<DownstreamState> firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
 
index 0cf4f9b663d36a72498e1d71a6420d6c6953cb63..e8b2abe80b01b7af23ebecf917c957f6d51d6d95 100644 (file)
@@ -1011,7 +1011,7 @@ public:
 
 struct ServerPool
 {
-  ServerPool(): d_servers(std::make_shared<ServerPolicy::NumberedServerVector>())
+  ServerPool(): d_servers(std::make_shared<const ServerPolicy::NumberedServerVector>())
   {
   }
 
@@ -1036,12 +1036,12 @@ struct ServerPool
 
   size_t poolLoad();
   size_t countServers(bool upOnly);
-  const std::shared_ptr<ServerPolicy::NumberedServerVector> getServers();
+  const std::shared_ptr<const ServerPolicy::NumberedServerVector> getServers();
   void addServer(shared_ptr<DownstreamState>& server);
   void removeServer(shared_ptr<DownstreamState>& server);
 
 private:
-  SharedLockGuarded<std::shared_ptr<ServerPolicy::NumberedServerVector>> d_servers;
+  SharedLockGuarded<std::shared_ptr<const ServerPolicy::NumberedServerVector>> d_servers;
   bool d_useECS{false};
 };
 
index 95ab0c4b4b24eca1a92a6cd75717fb3bbe71e687..005eaa3b8175d06bb63e85ed2b8011a42fab343c 100644 (file)
@@ -475,7 +475,7 @@ IDState* DownstreamState::getIDState(unsigned int& selectedID, int64_t& generati
 
 size_t ServerPool::countServers(bool upOnly)
 {
-  std::shared_ptr<ServerPolicy::NumberedServerVector> servers = nullptr;
+  std::shared_ptr<const ServerPolicy::NumberedServerVector> servers = nullptr;
   {
     auto lock = d_servers.read_lock();
     servers = *lock;
@@ -493,7 +493,7 @@ size_t ServerPool::countServers(bool upOnly)
 
 size_t ServerPool::poolLoad()
 {
-  std::shared_ptr<ServerPolicy::NumberedServerVector> servers = nullptr;
+  std::shared_ptr<const ServerPolicy::NumberedServerVector> servers = nullptr;
   {
     auto lock = d_servers.read_lock();
     servers = *lock;
@@ -507,9 +507,9 @@ size_t ServerPool::poolLoad()
   return load;
 }
 
-const std::shared_ptr<ServerPolicy::NumberedServerVector> ServerPool::getServers()
+const std::shared_ptr<const ServerPolicy::NumberedServerVector> ServerPool::getServers()
 {
-  std::shared_ptr<ServerPolicy::NumberedServerVector> result;
+  std::shared_ptr<const ServerPolicy::NumberedServerVector> result;
   {
     result = *(d_servers.read_lock());
   }
@@ -522,18 +522,18 @@ void ServerPool::addServer(shared_ptr<DownstreamState>& server)
   /* we can't update the content of the shared pointer directly even when holding the lock,
      as other threads might hold a copy. We can however update the pointer as long as we hold the lock. */
   unsigned int count = static_cast<unsigned int>((*servers)->size());
-  auto newServers = std::make_shared<ServerPolicy::NumberedServerVector>(*(*servers));
-  newServers->emplace_back(++count, server);
+  auto newServers = ServerPolicy::NumberedServerVector(*(*servers));
+  newServers.emplace_back(++count, server);
   /* we need to reorder based on the server 'order' */
-  std::stable_sort(newServers->begin(), newServers->end(), [](const std::pair<unsigned int,std::shared_ptr<DownstreamState> >& a, const std::pair<unsigned int,std::shared_ptr<DownstreamState> >& b) {
+  std::stable_sort(newServers.begin(), newServers.end(), [](const std::pair<unsigned int,std::shared_ptr<DownstreamState> >& a, const std::pair<unsigned int,std::shared_ptr<DownstreamState> >& b) {
       return a.second->d_config.order < b.second->d_config.order;
     });
   /* and now we need to renumber for Lua (custom policies) */
   size_t idx = 1;
-  for (auto& serv : *newServers) {
+  for (auto& serv : newServers) {
     serv.first = idx++;
   }
-  *servers = std::move(newServers);
+  *servers = std::make_shared<const ServerPolicy::NumberedServerVector>(std::move(newServers));
 }
 
 void ServerPool::removeServer(shared_ptr<DownstreamState>& server)
index f10365797d173fa4d9fcb0a1db7fc1655c40aa42..1e80eb756a18b89f3398b59c5a7f8234531d158b 100644 (file)
@@ -259,7 +259,7 @@ shared_ptr<DownstreamState> roundrobin(const ServerPolicy::NumberedServerVector&
   return servers.at(candidates.at((counter++) % candidates.size()) - 1).second;
 }
 
-const std::shared_ptr<ServerPolicy::NumberedServerVector> getDownstreamCandidates(const pools_t& pools, const std::string& poolName)
+const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const pools_t& pools, const std::string& poolName)
 {
   std::shared_ptr<ServerPool> pool = getPool(pools, poolName);
   return pool->getServers();