changeset 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 1249ab2f797c
children 048284447643
files mod_firewall/marks.lib.lua
diffstat 1 files changed, 5 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mod_firewall/marks.lib.lua	Thu Jun 08 19:15:12 2023 +0100
+++ b/mod_firewall/marks.lib.lua	Thu Jun 08 19:19:46 2023 +0100
@@ -3,21 +3,14 @@
 
 local user_sessions = prosody.hosts[module.host].sessions;
 
-module:hook("resource-bind", function (event)
-	local session = event.session;
-	local username = session.username;
-	local user = user_sessions[username];
-	local marks = user.firewall_marks;
-	if not marks then
-		marks = mark_storage:get(username) or {};
-		user.firewall_marks = marks; -- luacheck: ignore 122
-	end
-	session.firewall_marks = marks;
-end);
-
 module:hook("firewall/marked/user", function (event)
 	local user = user_sessions[event.username];
 	local marks = user and user.firewall_marks;
+	if user and not marks then
+		-- Load marks from storage to cache on the user object
+		marks = mark_storage:get(event.username) or {};
+		user.firewall_marks = marks; --luacheck: ignore 122
+	end
 	if marks then
 		marks[event.mark] = event.timestamp;
 	end