From: Remi Gacogne Date: Fri, 16 Apr 2021 13:40:34 +0000 (+0200) Subject: dnsdist: Move the Lua mutex to LockGuarded X-Git-Tag: dnsdist-1.7.0-alpha1~62^2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fcfffe21198103e04a489a875fc8fb9529f16033;p=thirdparty%2Fpdns.git dnsdist: Move the Lua mutex to LockGuarded --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 50679039d7..2ab26821c5 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -321,10 +321,10 @@ void doConsole() bool withReturn=true; retry:; try { - std::lock_guard lock(g_luamutex); + auto lua = g_lua.lock(); g_outputBuffer.clear(); resetLuaSideEffect(); - auto ret=g_lua.executeCode< + auto ret = lua->executeCode< boost::optional< boost::variant< string, @@ -779,11 +779,11 @@ static void controlClientThread(ConsoleConnection&& conn) bool withReturn=true; retry:; try { - std::lock_guard lock(g_luamutex); + auto lua = g_lua.lock(); g_outputBuffer.clear(); resetLuaSideEffect(); - auto ret=g_lua.executeCode< + auto ret = lua->executeCode< boost::optional< boost::variant< string, diff --git a/pdns/dnsdist-lua-actions.cc b/pdns/dnsdist-lua-actions.cc index 95a4d53913..236bd28fee 100644 --- a/pdns/dnsdist-lua-actions.cc +++ b/pdns/dnsdist-lua-actions.cc @@ -381,7 +381,7 @@ public: DNSAction::Action operator()(DNSQuestion* dq, std::string* ruleresult) const override { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); try { auto ret = d_func(dq); if (ruleresult) { @@ -418,7 +418,7 @@ public: {} DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); try { auto ret = d_func(dr); if (ruleresult) { @@ -460,8 +460,7 @@ public: { dnsdist_ffi_dnsquestion_t dqffi(dq); try { - std::lock_guard lock(g_luamutex); - + auto lock = g_lua.lock(); auto ret = d_func(&dqffi); if (ruleresult) { if (dqffi.result) { @@ -576,8 +575,7 @@ public: dnsdist_ffi_dnsquestion_t dqffi(dq); try { - std::lock_guard lock(g_luamutex); - + auto lock = g_lua.lock(); auto ret = d_func(&dqffi); if (ruleresult) { if (dqffi.result) { @@ -1269,7 +1267,7 @@ public: DnstapMessage message(data, !dq->getHeader()->qr ? DnstapMessage::MessageType::client_query : DnstapMessage::MessageType::client_response, d_identity, dq->remote, dq->local, protocol, reinterpret_cast(dq->getData().data()), dq->getData().size(), dq->queryTime, nullptr); { if (d_alterFunc) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); (*d_alterFunc)(dq, &message); } } @@ -1314,7 +1312,7 @@ public: #endif /* HAVE_LIBCRYPTO */ if (d_alterFunc) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); (*d_alterFunc)(dq, &message); } @@ -1403,7 +1401,7 @@ public: DnstapMessage message(data, DnstapMessage::MessageType::client_response, d_identity, dr->remote, dr->local, protocol, reinterpret_cast(dr->getData().data()), dr->getData().size(), dr->queryTime, &now); { if (d_alterFunc) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); (*d_alterFunc)(dr, &message); } } @@ -1448,7 +1446,7 @@ public: #endif /* HAVE_LIBCRYPTO */ if (d_alterFunc) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); (*d_alterFunc)(dr, &message); } diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 920fc78bb8..ba25be0765 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -728,8 +728,7 @@ catch (...) } } -std::mutex g_luamutex; -LuaContext g_lua; +LockGuarded g_lua{LuaContext()}; ComboAddress g_serverControl{"127.0.0.1:5199"}; @@ -853,7 +852,7 @@ static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const stru string qname = (*dq.qname).toLogString(); bool countQuery{true}; if(g_qcount.filter) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); std::tie (countQuery, qname) = g_qcount.filter(&dq); } @@ -1603,8 +1602,8 @@ static void maintThread() sleep(interval); { - std::lock_guard lock(g_luamutex); - auto f = g_lua.readVariable > >("maintenance"); + auto lua = g_lua.lock(); + auto f = lua->readVariable > >("maintenance"); if (f) { try { (*f)(); @@ -2217,7 +2216,7 @@ int main(int argc, char** argv) g_policy.setState(leastOutstandingPol); if(g_cmdLine.beClient || !g_cmdLine.command.empty()) { - setupLua(g_lua, true, false, g_cmdLine.config); + setupLua(*(g_lua.lock()), true, false, g_cmdLine.config); if (clientAddress != ComboAddress()) g_serverControl = clientAddress; doClient(g_serverControl, g_cmdLine.command); @@ -2243,7 +2242,7 @@ int main(int argc, char** argv) registerBuiltInWebHandlers(); if (g_cmdLine.checkConfig) { - setupLua(g_lua, false, true, g_cmdLine.config); + setupLua(*(g_lua.lock()), false, true, g_cmdLine.config); // No exception was thrown infolog("Configuration '%s' OK!", g_cmdLine.config); #ifdef COVERAGE @@ -2253,7 +2252,7 @@ int main(int argc, char** argv) #endif } - auto todo = setupLua(g_lua, false, false, g_cmdLine.config); + auto todo = setupLua(*(g_lua.lock()), false, false, g_cmdLine.config); auto localPools = g_pools.getCopy(); { diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index c9ef384522..74b0647cff 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -1080,8 +1080,7 @@ private: using servers_t =vector>; void responderThread(std::shared_ptr state); -extern std::mutex g_luamutex; -extern LuaContext g_lua; +extern LockGuarded g_lua; extern std::string g_outputBuffer; // locking for this is ok, as locked by g_luamutex class DNSRule diff --git a/pdns/dnsdistdist/dnsdist-healthchecks.cc b/pdns/dnsdistdist/dnsdist-healthchecks.cc index 4b1dd9057e..44629e7ba1 100644 --- a/pdns/dnsdistdist/dnsdist-healthchecks.cc +++ b/pdns/dnsdistdist/dnsdist-healthchecks.cc @@ -195,7 +195,7 @@ bool queueHealthCheck(std::shared_ptr& mplexer, const std::shared } if (ds->checkFunction) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); auto ret = ds->checkFunction(checkName, checkType, checkClass, &checkHeader); checkName = std::get<0>(ret); checkType = std::get<1>(ret); diff --git a/pdns/dnsdistdist/dnsdist-lbpolicies.cc b/pdns/dnsdistdist/dnsdist-lbpolicies.cc index d01ccd69ac..7138782774 100644 --- a/pdns/dnsdistdist/dnsdist-lbpolicies.cc +++ b/pdns/dnsdistdist/dnsdist-lbpolicies.cc @@ -329,7 +329,7 @@ std::shared_ptr ServerPolicy::getSelectedBackend(const ServerPo if (d_isLua) { if (!d_isFFI) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); selectedBackend = d_policy(servers, &dq); } else { @@ -338,7 +338,7 @@ std::shared_ptr ServerPolicy::getSelectedBackend(const ServerPo unsigned int selected = 0; if (!d_isPerThread) { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); selected = d_ffipolicy(&serversList, &dnsq); } else { diff --git a/pdns/dnsdistdist/dnsdist-rules.hh b/pdns/dnsdistdist/dnsdist-rules.hh index 0b42866341..c40519363c 100644 --- a/pdns/dnsdistdist/dnsdist-rules.hh +++ b/pdns/dnsdistdist/dnsdist-rules.hh @@ -1161,7 +1161,7 @@ public: bool matches(const DNSQuestion* dq) const override { try { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); return d_func(dq); } catch (const std::exception &e) { warnlog("LuaRule failed inside Lua: %s", e.what()); @@ -1190,7 +1190,7 @@ public: { dnsdist_ffi_dnsquestion_t dqffi(const_cast(dq)); try { - std::lock_guard lock(g_luamutex); + auto lock = g_lua.lock(); return d_func(&dqffi); } catch (const std::exception &e) { warnlog("LuaFFIRule failed inside Lua: %s", e.what()); diff --git a/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc b/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc index 29f297f72e..b1a31886e3 100644 --- a/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc +++ b/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc @@ -11,9 +11,8 @@ uint16_t g_maxOutstanding{std::numeric_limits::max()}; -std::mutex g_luamutex; #include "ext/luawrapper/include/LuaContext.hpp" -LuaContext g_lua; +LockGuarded g_lua{LuaContext()}; bool g_snmpEnabled{false}; bool g_snmpTrapsEnabled{false}; @@ -148,7 +147,7 @@ static void resetLuaContext() /* we need to reset this before cleaning the Lua state because the server policy might holds a reference to a Lua function (Lua policies) */ g_policy.setState(ServerPolicy("leastOutstanding", leastOutstanding, false)); - g_lua = LuaContext(); + *(g_lua.lock()) = LuaContext(); } BOOST_AUTO_TEST_SUITE(dnsdistlbpolicies) @@ -571,10 +570,10 @@ BOOST_AUTO_TEST_CASE(test_lua) { setServerPolicyLua("luaroundrobin", luaroundrobin) )foo"; resetLuaContext(); - g_lua.writeFunction("setServerPolicyLua", [](string name, ServerPolicy::policyfunc_t policy) { + g_lua.lock()->writeFunction("setServerPolicyLua", [](string name, ServerPolicy::policyfunc_t policy) { g_policy.setState(ServerPolicy{name, policy, true}); }); - g_lua.executeCode(policySetupStr); + g_lua.lock()->executeCode(policySetupStr); { ServerPolicy pol = g_policy.getCopy(); @@ -630,11 +629,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) { setServerPolicyLuaFFI("FFI round-robin", ffilb) )foo"; resetLuaContext(); - g_lua.executeCode(getLuaFFIWrappers()); - g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { + g_lua.lock()->executeCode(getLuaFFIWrappers()); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { g_policy.setState(ServerPolicy(name, policy)); }); - g_lua.executeCode(policySetupStr); + g_lua.lock()->executeCode(policySetupStr); { ServerPolicy pol = g_policy.getCopy(); @@ -687,11 +686,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) { setServerPolicyLuaFFI("FFI hashed", ffilb) )foo"; resetLuaContext(); - g_lua.executeCode(getLuaFFIWrappers()); - g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { + g_lua.lock()->executeCode(getLuaFFIWrappers()); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { g_policy.setState(ServerPolicy(name, policy)); }); - g_lua.executeCode(policySetupStr); + g_lua.lock()->executeCode(policySetupStr); { ServerPolicy pol = g_policy.getCopy(); @@ -742,11 +741,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) { setServerPolicyLuaFFI("FFI whashed", ffilb) )foo"; resetLuaContext(); - g_lua.executeCode(getLuaFFIWrappers()); - g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { + g_lua.lock()->executeCode(getLuaFFIWrappers()); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { g_policy.setState(ServerPolicy(name, policy)); }); - g_lua.executeCode(policySetupStr); + g_lua.lock()->executeCode(policySetupStr); { ServerPolicy pol = g_policy.getCopy(); @@ -800,11 +799,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) { setServerPolicyLuaFFI("FFI chashed", ffilb) )foo"; resetLuaContext(); - g_lua.executeCode(getLuaFFIWrappers()); - g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { + g_lua.lock()->executeCode(getLuaFFIWrappers()); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { g_policy.setState(ServerPolicy(name, policy)); }); - g_lua.executeCode(policySetupStr); + g_lua.lock()->executeCode(policySetupStr); { ServerPolicy pol = g_policy.getCopy();