From: Remi Gacogne Date: Tue, 16 Aug 2022 11:35:27 +0000 (+0200) Subject: dnsdist: Make ServerPolicy::NumberedServerVector const X-Git-Tag: rec-4.8.0-alpha1~58^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F11852%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Make ServerPolicy::NumberedServerVector const As suggested by Otto (Thanks!). --- diff --git a/pdns/dnsdist-lbpolicies.hh b/pdns/dnsdist-lbpolicies.hh index 395deec2b5..a1332c7729 100644 --- a/pdns/dnsdist-lbpolicies.hh +++ b/pdns/dnsdist-lbpolicies.hh @@ -96,7 +96,7 @@ void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr server); void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr server); -const std::shared_ptr getDownstreamCandidates(const map>& pools, const std::string& poolName); +const std::shared_ptr getDownstreamCandidates(const map>& pools, const std::string& poolName); std::shared_ptr firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq); diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 0cf4f9b663..e8b2abe80b 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -1011,7 +1011,7 @@ public: struct ServerPool { - ServerPool(): d_servers(std::make_shared()) + ServerPool(): d_servers(std::make_shared()) { } @@ -1036,12 +1036,12 @@ struct ServerPool size_t poolLoad(); size_t countServers(bool upOnly); - const std::shared_ptr getServers(); + const std::shared_ptr getServers(); void addServer(shared_ptr& server); void removeServer(shared_ptr& server); private: - SharedLockGuarded> d_servers; + SharedLockGuarded> d_servers; bool d_useECS{false}; }; diff --git a/pdns/dnsdistdist/dnsdist-backend.cc b/pdns/dnsdistdist/dnsdist-backend.cc index 95ab0c4b4b..005eaa3b81 100644 --- a/pdns/dnsdistdist/dnsdist-backend.cc +++ b/pdns/dnsdistdist/dnsdist-backend.cc @@ -475,7 +475,7 @@ IDState* DownstreamState::getIDState(unsigned int& selectedID, int64_t& generati size_t ServerPool::countServers(bool upOnly) { - std::shared_ptr servers = nullptr; + std::shared_ptr 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 servers = nullptr; + std::shared_ptr servers = nullptr; { auto lock = d_servers.read_lock(); servers = *lock; @@ -507,9 +507,9 @@ size_t ServerPool::poolLoad() return load; } -const std::shared_ptr ServerPool::getServers() +const std::shared_ptr ServerPool::getServers() { - std::shared_ptr result; + std::shared_ptr result; { result = *(d_servers.read_lock()); } @@ -522,18 +522,18 @@ void ServerPool::addServer(shared_ptr& 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((*servers)->size()); - auto newServers = std::make_shared(*(*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 >& a, const std::pair >& b) { + std::stable_sort(newServers.begin(), newServers.end(), [](const std::pair >& a, const std::pair >& 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(std::move(newServers)); } void ServerPool::removeServer(shared_ptr& server) diff --git a/pdns/dnsdistdist/dnsdist-lbpolicies.cc b/pdns/dnsdistdist/dnsdist-lbpolicies.cc index f10365797d..1e80eb756a 100644 --- a/pdns/dnsdistdist/dnsdist-lbpolicies.cc +++ b/pdns/dnsdistdist/dnsdist-lbpolicies.cc @@ -259,7 +259,7 @@ shared_ptr roundrobin(const ServerPolicy::NumberedServerVector& return servers.at(candidates.at((counter++) % candidates.size()) - 1).second; } -const std::shared_ptr getDownstreamCandidates(const pools_t& pools, const std::string& poolName) +const std::shared_ptr getDownstreamCandidates(const pools_t& pools, const std::string& poolName) { std::shared_ptr pool = getPool(pools, poolName); return pool->getServers();