From a2f87dd1ed14cdf215976f29daba10a0f793da40 Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Wed, 9 May 2018 16:26:39 +0200 Subject: [PATCH] rec: add lua maintenance callback --- pdns/lua-recursor4.cc | 8 ++++++++ pdns/lua-recursor4.hh | 3 +++ pdns/pdns_recursor.cc | 8 +++++++- pdns/recursordist/docs/lua-scripting/hooks.rst | 16 ++++++++++++++++ pdns/recursordist/docs/settings.rst | 13 +++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc index eb3f6d489a..cb16d9d3b3 100644 --- a/pdns/lua-recursor4.cc +++ b/pdns/lua-recursor4.cc @@ -402,12 +402,20 @@ void RecursorLua4::postLoad() { d_nxdomain = d_lw->readVariable>("nxdomain").get_value_or(0); d_postresolve = d_lw->readVariable>("postresolve").get_value_or(0); d_preoutquery = d_lw->readVariable>("preoutquery").get_value_or(0); + d_maintenance = d_lw->readVariable>("maintenance").get_value_or(0); d_ipfilter = d_lw->readVariable>("ipfilter").get_value_or(0); d_gettag = d_lw->readVariable>("gettag").get_value_or(0); d_gettag_ffi = d_lw->readVariable>("gettag_ffi").get_value_or(0); } +void RecursorLua4::maintenance() const +{ + if (d_maintenance) { + d_maintenance(); + } +} + bool RecursorLua4::prerpz(DNSQuestion& dq, int& ret) const { return genhook(d_prerpz, dq, ret); diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh index 814f8adcd7..9130bac5fa 100644 --- a/pdns/lua-recursor4.hh +++ b/pdns/lua-recursor4.hh @@ -112,6 +112,7 @@ public: unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector* policyTags, LuaContext::LuaObject& data, const std::map&, bool tcp, std::string& requestorId, std::string& deviceId) const; unsigned int gettag_ffi(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector* policyTags, LuaContext::LuaObject& data, const std::map&, bool tcp, std::string& requestorId, std::string& deviceId, uint32_t& ttlCap, bool& variable) const; + void maintenance() const; bool prerpz(DNSQuestion& dq, int& ret) const; bool preresolve(DNSQuestion& dq, int& ret) const; bool nxdomain(DNSQuestion& dq, int& ret) const; @@ -139,6 +140,8 @@ protected: virtual void postPrepareContext() override; virtual void postLoad() override; private: + typedef std::function luamaintenance_t; + luamaintenance_t d_maintenance; typedef std::function luacall_t; luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery; bool genhook(const luacall_t& func, DNSQuestion& dq, int& ret) const; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index dac86fb0c8..000fc9960d 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -3518,8 +3518,9 @@ try bool listenOnTCP(true); time_t last_stat = 0; - time_t last_carbon=0; + time_t last_carbon=0, last_lua_maintenance=0; time_t carbonInterval=::arg().asNum("carbon-interval"); + time_t luaMaintenanceInterval=::arg().asNum("lua-maintenance-interval"); counter.store(0); // used to periodically execute certain tasks for(;;) { while(MT->schedule(&g_now)); // MTasker letting the mthreads do their thing @@ -3555,6 +3556,10 @@ try last_carbon = g_now.tv_sec; } } + if(!t_id && (g_now.tv_sec - last_lua_maintenance >= luaMaintenanceInterval)) { + t_pdl->maintenance(); + last_lua_maintenance = g_now.tv_sec; + } t_fdm->run(&g_now); // 'run' updates g_now for us @@ -3683,6 +3688,7 @@ int main(int argc, char **argv) ::arg().set("etc-hosts-file", "Path to 'hosts' file")="/etc/hosts"; ::arg().set("serve-rfc1918", "If we should be authoritative for RFC 1918 private IP space")="yes"; ::arg().set("lua-dns-script", "Filename containing an optional 'lua' script that will be used to modify dns answers")=""; + ::arg().set("lua-maintenance-interval", "Number of seconds between calls to the lua user defined maintenance() function")="1"; ::arg().set("latency-statistic-size","Number of latency values to calculate the qa-latency average")="10000"; ::arg().setSwitch( "disable-packetcache", "Disable packetcache" )= "no"; ::arg().set("ecs-ipv4-bits", "Number of bits of IPv4 address to pass for EDNS Client Subnet")="24"; diff --git a/pdns/recursordist/docs/lua-scripting/hooks.rst b/pdns/recursordist/docs/lua-scripting/hooks.rst index 29a196df9f..f2148ccddc 100644 --- a/pdns/recursordist/docs/lua-scripting/hooks.rst +++ b/pdns/recursordist/docs/lua-scripting/hooks.rst @@ -321,3 +321,19 @@ MIB ^^^ .. literalinclude:: ../../RECURSOR-MIB.txt + +.. _hooks-maintenance-callback: + +Maintenance callback +-------------------- +Starting with version 4.1.3 of the recursor, it is possible to define a `maintenance()` callback function that will be called periodically. +This function expects no argument and doesn't return any value + +.. code-block:: Lua + + function maintenance() + -- This would be called every second + -- Perform here your maintenance + end + +The interval can be configured through the :ref:`setting-maintenance-interval` setting. diff --git a/pdns/recursordist/docs/settings.rst b/pdns/recursordist/docs/settings.rst index ca19519dfb..aa0be9b7c7 100644 --- a/pdns/recursordist/docs/settings.rst +++ b/pdns/recursordist/docs/settings.rst @@ -652,6 +652,19 @@ See :doc:`lua-config/index` for the options that can be set in this file. Path to a lua file to manipulate the Recursor's answers. See :doc:`lua-scripting/index` for more information. +.. _setting-maintenance-interval: + +``lua-maintenance-interval`` +------------------- +.. versionadded:: 4.1.3 + +- Integer +- Default: 1 + + +The interval between calls to the Lua user defined `maintenance()` function in seconds. +See :ref:`hooks-maintenance-callback` + .. _setting-max-cache-entries: ``max-cache-entries`` -- 2.47.2