commit 60c10d5: [Project] Fallback to callbacks from coroutines

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Feb 28 20:14:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-02-28 19:54:43 +0000
URL: https://github.com/rspamd/rspamd/commit/60c10d57b181b24c1993d1148186d579e4682fd2

[Project] Fallback to callbacks from coroutines

---
 src/plugins/lua/multimap.lua   | 39 ++++++++++++++-------------
 src/plugins/lua/reputation.lua | 60 ++++++++++++++++++++++--------------------
 2 files changed, 52 insertions(+), 47 deletions(-)

diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua
index 9eda5aeec..aa3e4d04a 100644
--- a/src/plugins/lua/multimap.lua
+++ b/src/plugins/lua/multimap.lua
@@ -28,7 +28,6 @@ local regexp = require "rspamd_regexp"
 local rspamd_expression = require "rspamd_expression"
 local rspamd_ip = require "rspamd_ip"
 local lua_util = require "lua_util"
-local rspamd_dns = require "rspamd_dns"
 local lua_selectors = require "lua_selectors"
 local redis_params
 local fun = require "fun"
@@ -733,26 +732,30 @@ local function multimap_callback(task, rule)
       local ip = task:get_from_ip()
       if ip:is_valid() then
         local to_resolve = ip_to_rbl(ip, rule['map'])
+        local function dns_cb(_, _, results, err)
+          lua_util.debugm(N, rspamd_config,
+              'resolve() finished: results=%1, err=%2, to_resolve=%3',
+              results, err, to_resolve)
+
+          if err and
+              (err ~= 'requested record is not found' and
+                  err ~= 'no records with this name') then
+            rspamd_logger.errx(task, 'error looking up %s: %s', to_resolve, results)
+          elseif results then
+            task:insert_result(rule['symbol'], 1, rule['map'])
+            if pre_filter then
+              task:set_pre_result(rule['action'],
+                  'Matched map: ' .. rule['symbol'], N)
+            end
+          end
+        end
 
-        local is_ok, results = rspamd_dns.request({
-          type = "a",
-          task = task,
+        task:get_resolver():resolve_a({
+          task= task,
           name = to_resolve,
+          callback = dns_cb,
+          forced = true
         })
-
-        lua_util.debugm(N, rspamd_config,
-            'resolve() finished: results=%1, is_ok=%2, to_resolve=%3',
-            results, is_ok, to_resolve)
-
-        if not is_ok and
-            (results ~= 'requested record is not found' and results ~= 'no records with this name') then
-          rspamd_logger.errx(task, 'error looking up %s: %s', to_resolve, results)
-        elseif is_ok then
-          task:insert_result(rule['symbol'], 1, rule['map'])
-          if pre_filter then
-            task:set_pre_result(rule['action'], 'Matched map: ' .. rule['symbol'], N)
-          end
-        end
       end
     end,
     header = function()
diff --git a/src/plugins/lua/reputation.lua b/src/plugins/lua/reputation.lua
index 374771c9b..e91c6ebb7 100644
--- a/src/plugins/lua/reputation.lua
+++ b/src/plugins/lua/reputation.lua
@@ -25,7 +25,6 @@ local N = 'reputation'
 
 local rspamd_logger = require "rspamd_logger"
 local rspamd_util = require "rspamd_util"
-local rspamd_dns = require "rspamd_dns"
 local lua_util = require "lua_util"
 local lua_maps = require "lua_maps"
 local hash = require 'rspamd_cryptobox_hash'
@@ -857,39 +856,42 @@ local function reputation_dns_get_token(task, rule, token, continuation_cb)
   local key = gen_token_key(token, rule)
   local dns_name = key .. '.' .. rule.backend.config.list
 
-  local is_ok, results = rspamd_dns.request({
-    type = 'a',
-    task = task,
-    name = dns_name,
-    forced = true,
-  })
-
-  if not is_ok and (results ~= 'requested record is not found' and results ~= 'no records with this name') then
-    rspamd_logger.errx(task, 'error looking up %s: %s', dns_name, results)
-  end
+  local function dns_cb(_, _, results, err)
+    if err and (err ~= 'requested record is not found' and
+        err ~= 'no records with this name') then
+      rspamd_logger.errx(task, 'error looking up %s: %s', dns_name, err)
+    end
 
-  lua_util.debugm(N, task, 'DNS RESPONSE: label=%1 results=%2 is_ok=%3 list=%4',
-    dns_name, results, is_ok, rule.backend.config.list)
+    lua_util.debugm(N, task, 'DNS RESPONSE: label=%1 results=%2 err=%3 list=%4',
+        dns_name, results, err, rule.backend.config.list)
 
-  -- Now split tokens to list of values
-  if is_ok  then
-    local values = {}
-    -- Format: key1=num1;key2=num2...keyn=numn
-    fun.each(function(e)
-      local vals = lua_util.rspamd_str_split(e, "=")
-      if vals and #vals == 2 then
-        local nv = tonumber(vals[2])
-        if nv then
-          values[vals[1]] = nv
+    -- Now split tokens to list of values
+    if results  then
+      local values = {}
+      -- Format: key1=num1;key2=num2...keyn=numn
+      fun.each(function(e)
+        local vals = lua_util.rspamd_str_split(e, "=")
+        if vals and #vals == 2 then
+          local nv = tonumber(vals[2])
+          if nv then
+            values[vals[1]] = nv
+          end
         end
-      end
-    end,
-    lua_util.rspamd_str_split(results[1], ";"))
+      end,
+          lua_util.rspamd_str_split(results[1], ";"))
 
-    continuation_cb(nil, dns_name, values)
-  else
-    continuation_cb(results, dns_name, nil)
+      continuation_cb(nil, dns_name, values)
+    else
+      continuation_cb(results, dns_name, nil)
+    end
   end
+
+ task:get_resolver():resolve_a({
+    task = task,
+    name = dns_name,
+    callback = dns_cb,
+    forced = true,
+  })
 end
 
 local function reputation_redis_init(rule, cfg, ev_base, worker)


More information about the Commits mailing list