]> granicus.if.org Git - pdns/commitdiff
rec: add lua maintenance callback
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Wed, 9 May 2018 14:26:39 +0000 (16:26 +0200)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Thu, 17 May 2018 16:31:57 +0000 (18:31 +0200)
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh
pdns/pdns_recursor.cc
pdns/recursordist/docs/lua-scripting/hooks.rst
pdns/recursordist/docs/settings.rst

index eb3f6d489a83fb74ab25bdbb183c21d676c06cbb..cb16d9d3b30552b5e278c98207fe39e989c2fdae 100644 (file)
@@ -402,12 +402,20 @@ void RecursorLua4::postLoad() {
   d_nxdomain = d_lw->readVariable<boost::optional<luacall_t>>("nxdomain").get_value_or(0);
   d_postresolve = d_lw->readVariable<boost::optional<luacall_t>>("postresolve").get_value_or(0);
   d_preoutquery = d_lw->readVariable<boost::optional<luacall_t>>("preoutquery").get_value_or(0);
+  d_maintenance = d_lw->readVariable<boost::optional<luamaintenance_t>>("maintenance").get_value_or(0);
 
   d_ipfilter = d_lw->readVariable<boost::optional<ipfilter_t>>("ipfilter").get_value_or(0);
   d_gettag = d_lw->readVariable<boost::optional<gettag_t>>("gettag").get_value_or(0);
   d_gettag_ffi = d_lw->readVariable<boost::optional<gettag_ffi_t>>("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);
index 814f8adcd7e4d8353c29da58e57acd9aca59d50a..9130bac5faf846c559f69152ea8764d0f3df3100 100644 (file)
@@ -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<std::string>* policyTags, LuaContext::LuaObject& data, const std::map<uint16_t, EDNSOptionView>&, 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<std::string>* policyTags, LuaContext::LuaObject& data, const std::map<uint16_t, EDNSOptionView>&, 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<void()> luamaintenance_t;
+  luamaintenance_t d_maintenance;
   typedef std::function<bool(DNSQuestion*)> 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;
index dac86fb0c85f87a80861270b9e7af070b1c8b321..000fc9960d53dc28c2b76caf4e98db17d7531c77 100644 (file)
@@ -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";
index 29a196df9fc9a30269327f619f7a358dc86e99c8..f2148ccddc01d8937eb2600c85992fa3adb299fb 100644 (file)
@@ -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.
index ca19519dfb4cb4e3a11ba5a79364e5390fbd6bde..aa0be9b7c7583aa6bdb9722c8dd07290df77c534 100644 (file)
@@ -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``