]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daf: adapt to changes in map result handling
authorPetr Špaček <petr.spacek@nic.cz>
Wed, 14 Oct 2020 15:22:51 +0000 (17:22 +0200)
committerTomas Krizek <tomas.krizek@nic.cz>
Mon, 26 Oct 2020 13:25:14 +0000 (14:25 +0100)
modules/daf/daf.lua

index 6cdf5dfecca38b7482f860fdb6e3c6f750f5ef96..a658fb4016c9227d6fdba387d42ca462085ba7cf 100644 (file)
@@ -191,6 +191,7 @@ function M.del(id)
                        return true
                end
        end
+       return nil
 end
 
 -- @function Find a rule
@@ -200,6 +201,7 @@ function M.get(id)
                        return r
                end
        end
+       return nil
 end
 
 -- @function Enable/disable a rule
@@ -210,6 +212,7 @@ function M.toggle(id, val)
                        return true
                end
        end
+       return nil
 end
 
 -- @function Enable/disable a rule
@@ -221,14 +224,10 @@ function M.enable(id)
 end
 
 local function consensus(op, ...)
-       local ret = false
        local results = map(string.format(op, ...))
-       for idx, r in ipairs(results) do
-               if idx == 1 then
-                       -- non-empty table, init to true
-                       ret = true
-               end
-               ret = ret and r
+       local ret = results.n > 0  -- init to true for non-empty results
+       for idx=1, results.n do
+               ret = ret and results[idx]
        end
        return ret
 end
@@ -270,8 +269,9 @@ local function api(h, stream)
                if query then
                        local ok, r = pcall(M.add, query)
                        if not ok then return 500, string.format('"%s"', r:match('/([^/]+)$')) end
-                       -- Dispatch to all other workers
-                       consensus('daf.add "%s"', query)
+                       -- Dispatch to all other workers:
+                       -- we ignore return values except error() because they are not serializable
+                       consensus('daf.add "%s" and true', query)
                        return rule_info(r)
                end
                return 400
@@ -298,11 +298,15 @@ end
 
 local function getmatches()
        local update = {}
-       for _, rules in ipairs(map 'daf.rules') do
-               for _, r in ipairs(rules) do
-                       local id = tostring(r.rule.id)
-                       -- Must have string keys for JSON object and not an array
-                       update[id] = (update[id] or 0) + r.rule.count
+       -- Must have string keys for JSON object and not an array
+       local inst_counters = map('ret = {} '
+               .. 'for _, rule in ipairs(daf.rules) do '
+                       .. 'ret[tostring(rule.rule.id)] = rule.rule.count '
+               .. 'end '
+               .. 'return ret')
+       for inst_idx=1, inst_counters.n do
+               for r_id, r_cnt in pairs(inst_counters[inst_idx]) do
+                       update[r_id] = (update[r_id] or 0) + r_cnt
                end
        end
        return update