mod_pep: Limit possible growth of number of pubsub services 0.11
authorKim Alvefur <zash@zash.se>
Thu, 04 Nov 2021 00:35:44 +0100
branch0.11
changeset 12090 1dc00ca6ee9d
parent 12089 1d213c6f781b
child 12091 19f67d44ec37
mod_pep: Limit possible growth of number of pubsub services
plugins/mod_pep.lua
--- a/plugins/mod_pep.lua	Thu Nov 04 00:33:58 2021 +0100
+++ b/plugins/mod_pep.lua	Thu Nov 04 00:35:44 2021 +0100
@@ -18,8 +18,20 @@
 
 local empty_set = set_new();
 
+-- username -> object passed to module:add_items()
+local pep_service_items = {};
+
+-- size of caches with full pubsub service objects
+local service_cache_size = module:get_option_number("pep_service_cache_size", 1000);
+
 -- username -> util.pubsub service object
-local services = {};
+local services = cache.new(service_cache_size, function (username, _)
+	local item = pep_service_items[username];
+	pep_service_items[username] = nil;
+	if item then
+		module:remove_item("pep-service", item);
+	end
+end):table();
 
 -- username -> recipient -> set of nodes
 local recipients = {};
@@ -202,7 +214,9 @@
 		check_node_config = check_node_config;
 	});
 	services[username] = service;
-	module:add_item("pep-service", { service = service, jid = user_bare });
+	local item = { service = service, jid = user_bare }
+	pep_service_items[username] = item;
+	module:add_item("pep-service", item);
 	return service;
 end