Debian Codegraph @

lualib/lua_fuzzy_redis.lua

raw
1 --[[
2 Copyright (c) 2026, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]
16
17 -- Lua module for fuzzy Redis backend update path (following the Bayes pattern)
18
19 local exports = {}
20 local lua_redis = require "lua_redis"
21 local logger = require "rspamd_logger"
22
23 local N = "fuzzy_redis"
24
25 local function gen_update_functor(redis_params, update_script_id)
26 -- Returns function(ev_base, prefix, updates, src, expire, callback)
27 -- updates is an array of tables: {op, digest, flag, value, is_weak, shingle_keys}
28 -- callback(success_boolean) is called when all operations complete
29 return function(ev_base, prefix, updates, src, expire, callback)
30 local n_ops = 0
31 local n_completed = 0
32 local has_error = false
33 local count_key = prefix .. "_count"
34
35 -- Count actual operations (skip "dup")
36 for _, upd in ipairs(updates) do
37 if upd.op ~= "dup" then
38 n_ops = n_ops + 1
39 end
40 end
41
42 -- Final step: INCR version key and invoke callback
43 local function do_version_incr()
44 local version_key = prefix .. src
45
46 local function version_cb(err, _)
47 if err then
48 logger.errx(rspamd_config, '%s: version INCR failed for %s: %s',
49 N, version_key, err)
50 end
51 callback(not has_error)
52 end
53
54 if not lua_redis.redis_make_request_taskless(ev_base, rspamd_config,
55 redis_params, version_key, true, version_cb, 'INCR', { version_key }) then
56 logger.errx(rspamd_config, '%s: cannot make version INCR request', N)
57 callback(false)
58 end
59 end
60
61 -- Called when one exec_redis_script completes
62 local function on_op_complete()
63 n_completed = n_completed + 1
64 if n_completed >= n_ops then
65 do_version_incr()
66 end
67 end
68
69 -- If no actual operations, just do version INCR
70 if n_ops == 0 then
71 do_version_incr()
72 return
73 end
74
75 for _, upd in ipairs(updates) do
76 if upd.op ~= "dup" then
77 local hash_key = prefix .. upd.digest
78
79 -- Build KEYS array (only actual Redis key names) and ARGV (parameters)
80 local keys = {
81 hash_key,
82 count_key,
83 }
84
85 -- Append shingle keys if present
86 if upd.shingle_keys then
87 for _, sk in ipairs(upd.shingle_keys) do
88 keys[#keys + 1] = sk
89 end
90 end
91
92 local args = {
93 upd.op,
94 tostring(upd.flag),
95 tostring(upd.value),
96 tostring(expire),
97 tostring(upd.timestamp),
98 tostring(upd.is_weak),
99 upd.digest,
100 }
101
102 local function update_cb(err, _)
103 if err then
104 logger.errx(rspamd_config, '%s: update script failed: %s', N, err)
105 has_error = true
106 end
107 on_op_complete()
108 end
109
110 lua_redis.exec_redis_script(update_script_id,
111 { ev_base = ev_base, is_write = true, key = hash_key },
112 update_cb, keys, args)
113 end
114 end
115 end
116 end
117
118 -- Initialize fuzzy Redis update module
119 -- @param redis_params table returned by lua_redis.try_load_redis_servers
120 -- @return update functor or nil on error
121 exports.lua_fuzzy_redis_init = function(redis_params)
122 if not redis_params then
123 logger.errx(rspamd_config, '%s: no redis params provided', N)
124 return nil
125 end
126
127 local update_script_id, err = lua_redis.load_redis_script_from_file(
128 "fuzzy_update.lua", redis_params)
129 if not update_script_id then
130 logger.errx(rspamd_config, '%s: cannot load fuzzy_update.lua: %s', N,
131 err or "unknown error")
132 return nil
133 end
134
135 return gen_update_functor(redis_params, update_script_id)
136 end
137
138 return exports