bool withReturn=true;
retry:;
try {
- std::lock_guard<std::mutex> 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,
bool withReturn=true;
retry:;
try {
- std::lock_guard<std::mutex> 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,
DNSAction::Action operator()(DNSQuestion* dq, std::string* ruleresult) const override
{
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
try {
auto ret = d_func(dq);
if (ruleresult) {
{}
DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
{
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
try {
auto ret = d_func(dr);
if (ruleresult) {
{
dnsdist_ffi_dnsquestion_t dqffi(dq);
try {
- std::lock_guard<std::mutex> lock(g_luamutex);
-
+ auto lock = g_lua.lock();
auto ret = d_func(&dqffi);
if (ruleresult) {
if (dqffi.result) {
dnsdist_ffi_dnsquestion_t dqffi(dq);
try {
- std::lock_guard<std::mutex> lock(g_luamutex);
-
+ auto lock = g_lua.lock();
auto ret = d_func(&dqffi);
if (ruleresult) {
if (dqffi.result) {
DnstapMessage message(data, !dq->getHeader()->qr ? DnstapMessage::MessageType::client_query : DnstapMessage::MessageType::client_response, d_identity, dq->remote, dq->local, protocol, reinterpret_cast<const char*>(dq->getData().data()), dq->getData().size(), dq->queryTime, nullptr);
{
if (d_alterFunc) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
(*d_alterFunc)(dq, &message);
}
}
#endif /* HAVE_LIBCRYPTO */
if (d_alterFunc) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
(*d_alterFunc)(dq, &message);
}
DnstapMessage message(data, DnstapMessage::MessageType::client_response, d_identity, dr->remote, dr->local, protocol, reinterpret_cast<const char*>(dr->getData().data()), dr->getData().size(), dr->queryTime, &now);
{
if (d_alterFunc) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
(*d_alterFunc)(dr, &message);
}
}
#endif /* HAVE_LIBCRYPTO */
if (d_alterFunc) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
(*d_alterFunc)(dr, &message);
}
}
}
-std::mutex g_luamutex;
-LuaContext g_lua;
+LockGuarded<LuaContext> g_lua{LuaContext()};
ComboAddress g_serverControl{"127.0.0.1:5199"};
string qname = (*dq.qname).toLogString();
bool countQuery{true};
if(g_qcount.filter) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
std::tie (countQuery, qname) = g_qcount.filter(&dq);
}
sleep(interval);
{
- std::lock_guard<std::mutex> lock(g_luamutex);
- auto f = g_lua.readVariable<boost::optional<std::function<void()> > >("maintenance");
+ auto lua = g_lua.lock();
+ auto f = lua->readVariable<boost::optional<std::function<void()> > >("maintenance");
if (f) {
try {
(*f)();
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);
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
#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();
{
using servers_t =vector<std::shared_ptr<DownstreamState>>;
void responderThread(std::shared_ptr<DownstreamState> state);
-extern std::mutex g_luamutex;
-extern LuaContext g_lua;
+extern LockGuarded<LuaContext> g_lua;
extern std::string g_outputBuffer; // locking for this is ok, as locked by g_luamutex
class DNSRule
}
if (ds->checkFunction) {
- std::lock_guard<std::mutex> 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);
if (d_isLua) {
if (!d_isFFI) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
selectedBackend = d_policy(servers, &dq);
}
else {
unsigned int selected = 0;
if (!d_isPerThread) {
- std::lock_guard<std::mutex> lock(g_luamutex);
+ auto lock = g_lua.lock();
selected = d_ffipolicy(&serversList, &dnsq);
}
else {
bool matches(const DNSQuestion* dq) const override
{
try {
- std::lock_guard<std::mutex> 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());
{
dnsdist_ffi_dnsquestion_t dqffi(const_cast<DNSQuestion*>(dq));
try {
- std::lock_guard<std::mutex> 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());
uint16_t g_maxOutstanding{std::numeric_limits<uint16_t>::max()};
-std::mutex g_luamutex;
#include "ext/luawrapper/include/LuaContext.hpp"
-LuaContext g_lua;
+LockGuarded<LuaContext> g_lua{LuaContext()};
bool g_snmpEnabled{false};
bool g_snmpTrapsEnabled{false};
/* 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)
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();
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();
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();
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();
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();