{ "addLocal", true, R"(addr [, {doTCP=true, reusePort=false, tcpFastOpenQueueSize=0, interface="", cpus={}}])", "add `addr` to the list of addresses we listen on" },
{ "addCacheHitResponseAction", true, R"(DNS rule, DNS response action [, {uuid="UUID", name="name"}}])", "add a cache hit response rule" },
{ "addCacheInsertedResponseAction", true, R"(DNS rule, DNS response action [, {uuid="UUID", name="name"}}])", "add a cache inserted response rule" },
+ { "addMaintenanceCallback", true, "callback", "register a function to be called as part of the maintenance hook, every second" },
{ "addResponseAction", true, R"(DNS rule, DNS response action [, {uuid="UUID", name="name"}}])", "add a response rule" },
{ "addSelfAnsweredResponseAction", true, R"(DNS rule, DNS response action [, {uuid="UUID", name="name"}}])", "add a self-answered response rule" },
{ "addTLSLocal", true, "addr, certFile(s), keyFile(s) [,params]", "listen to incoming DNS over TLS queries on the specified address using the specified certificate (or list of) and key (or list of). The last parameter is a table" },
#include "dnsdist-ecs.hh"
#include "dnsdist-healthchecks.hh"
#include "dnsdist-lua.hh"
+#include "dnsdist-lua-hooks.hh"
#include "xsk.hh"
#ifdef LUAJIT_VERSION
#include "dnsdist-lua-ffi.hh"
setupLuaBindingsPacketCache(luaCtx, client);
setupLuaBindingsProtoBuf(luaCtx, client, configCheck);
setupLuaBindingsRings(luaCtx, client);
+ dnsdist::lua::hooks::setupLuaHooks(luaCtx);
setupLuaInspection(luaCtx);
setupLuaRules(luaCtx);
setupLuaVars(luaCtx);
#include "dnsdist-edns.hh"
#include "dnsdist-healthchecks.hh"
#include "dnsdist-lua.hh"
+#include "dnsdist-lua-hooks.hh"
#include "dnsdist-nghttp2.hh"
#include "dnsdist-proxy-protocol.hh"
#include "dnsdist-random.hh"
{
auto lua = g_lua.lock();
- auto f = lua->readVariable<boost::optional<std::function<void()> > >("maintenance");
- if (f) {
- try {
+ try {
+ auto f = lua->readVariable<boost::optional<std::function<void()> > >("maintenance");
+ if (f) {
(*f)();
- secondsToWaitLog = 0;
}
- catch (const std::exception &e) {
- if (secondsToWaitLog <= 0) {
- warnlog("Error during execution of maintenance function: %s", e.what());
- secondsToWaitLog = 61;
- }
- secondsToWaitLog -= interval;
+ dnsdist::lua::hooks::runMaintenanceHook(*lua);
+ secondsToWaitLog = 0;
+ }
+ catch (const std::exception &e) {
+ if (secondsToWaitLog <= 0) {
+ warnlog("Error during execution of maintenance function(s): %s", e.what());
+ secondsToWaitLog = 61;
}
+ secondsToWaitLog -= interval;
}
}
dnsdist-lua-bindings.cc \
dnsdist-lua-ffi-interface.h dnsdist-lua-ffi-interface.inc \
dnsdist-lua-ffi.cc dnsdist-lua-ffi.hh \
+ dnsdist-lua-hooks.cc dnsdist-lua-hooks.hh \
dnsdist-lua-inspection-ffi.cc dnsdist-lua-inspection-ffi.h \
dnsdist-lua-inspection.cc \
dnsdist-lua-network.cc dnsdist-lua-network.hh \
--- /dev/null
+
+#include "dnsdist-lua-hooks.hh"
+#include "dnsdist-lua.hh"
+#include "lock.hh"
+
+namespace dnsdist::lua::hooks
+{
+static LockGuarded<std::vector<MaintenanceCallback>> s_maintenanceHook;
+
+void runMaintenanceHook(const LuaContext& context)
+{
+ (void)context;
+ for (const auto& callback : *(s_maintenanceHook.lock())) {
+ callback();
+ }
+}
+
+void addMaintenanceCallback(const LuaContext& context, MaintenanceCallback callback)
+{
+ (void)context;
+ s_maintenanceHook.lock()->push_back(std::move(callback));
+}
+
+void setupLuaHooks(LuaContext& luaCtx)
+{
+ luaCtx.writeFunction("addMaintenanceCallback", [&luaCtx](const MaintenanceCallback& callback) {
+ setLuaSideEffect();
+ addMaintenanceCallback(luaCtx, callback);
+ });
+}
+
+}
--- /dev/null
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#pragma once
+
+#include <functional>
+
+class LuaContext;
+
+namespace dnsdist::lua::hooks
+{
+using MaintenanceCallback = std::function<void()>;
+void runMaintenanceHook(const LuaContext& context);
+void addMaintenanceCallback(MaintenanceCallback callback);
+void setupLuaHooks(LuaContext& luaCtx);
+}
Other functions
---------------
+.. function:: addMaintenanceCallback(callback)
+
+ .. versionadded:: 1.10.0
+
+ Register a Lua function to be called as part of the ``maintenance`` hook, which is executed roughly every second.
+ The function should not block for a long period of time, as it would otherwise delay the execution of the other functions registered for this hook, as well as the execution of the :func:`maintenance` function.
+
+ :param function callback: The function to be called. It takes no parameter and returns no value.
+
+ .. code-block:: lua
+
+ function myCallback(hostname, ips)
+ print('called')
+ end
+ addMaintenanceCallback(myCallback)
+
+
.. function:: getAddressInfo(hostname, callback)
.. versionadded:: 1.9.0
If this function exists, it is called every second to do regular tasks.
This can be used for e.g. :doc:`Dynamic Blocks <../guides/dynblocks>`.
+ See also :func:`addMaintenanceCallback`.
.. function:: threadmessage(cmd, dict)
local last = 0
serial = %d
- function maintenance()
+ function reloadCallback()
local now = os.time()
if ((now - last) > 2) then
serial = serial + 1
last = now
end
end
+ addMaintenanceCallback(reloadCallback)
"""
_config_params = ['_consoleKeyB64', '_consolePort', '_resolverCertificateSerial', '_resolverCertificateValidFrom', '_resolverCertificateValidUntil', '_dnsDistPortDNSCrypt', '_providerName', '_testServerPort', '_resolverCertificateSerial']