comparison mod_firewall/marks.lib.lua @ 5541:3804ee5117ca

mod_firewall: Load marks from storage on demand rather than at login This ensures people who don't use marks, or use them infrequently, don't pay a perf cost on every resource bind.
author Matthew Wild <mwild1@gmail.com>
date Thu, 08 Jun 2023 19:19:46 +0100
parents 96dec7681af8
children 048284447643
comparison
equal deleted inserted replaced
5540:1249ab2f797c 5541:3804ee5117ca
1 local mark_storage = module:open_store("firewall_marks"); 1 local mark_storage = module:open_store("firewall_marks");
2 local mark_map_storage = module:open_store("firewall_marks", "map"); 2 local mark_map_storage = module:open_store("firewall_marks", "map");
3 3
4 local user_sessions = prosody.hosts[module.host].sessions; 4 local user_sessions = prosody.hosts[module.host].sessions;
5 5
6 module:hook("resource-bind", function (event)
7 local session = event.session;
8 local username = session.username;
9 local user = user_sessions[username];
10 local marks = user.firewall_marks;
11 if not marks then
12 marks = mark_storage:get(username) or {};
13 user.firewall_marks = marks; -- luacheck: ignore 122
14 end
15 session.firewall_marks = marks;
16 end);
17
18 module:hook("firewall/marked/user", function (event) 6 module:hook("firewall/marked/user", function (event)
19 local user = user_sessions[event.username]; 7 local user = user_sessions[event.username];
20 local marks = user and user.firewall_marks; 8 local marks = user and user.firewall_marks;
9 if user and not marks then
10 -- Load marks from storage to cache on the user object
11 marks = mark_storage:get(event.username) or {};
12 user.firewall_marks = marks; --luacheck: ignore 122
13 end
21 if marks then 14 if marks then
22 marks[event.mark] = event.timestamp; 15 marks[event.mark] = event.timestamp;
23 end 16 end
24 local ok, err = mark_map_storage:set(event.username, event.mark, event.timestamp); 17 local ok, err = mark_map_storage:set(event.username, event.mark, event.timestamp);
25 if not ok then 18 if not ok then