mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
authorKim Alvefur <zash@zash.se>
Sun, 03 Mar 2024 11:23:40 +0100
changeset 5857 97c9b76867ca
parent 5856 133b23758cf6
child 5858 fcfe490de8a4
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel) Otherwise the global event handlers accumulate, one added each time logging is reoladed, and each invocation of the signal or event triggers one dump of each created ringbuffer.
mod_log_ringbuffer/mod_log_ringbuffer.lua
--- a/mod_log_ringbuffer/mod_log_ringbuffer.lua	Sat Mar 02 15:11:20 2024 +0100
+++ b/mod_log_ringbuffer/mod_log_ringbuffer.lua	Sun Mar 03 11:23:40 2024 +0100
@@ -90,6 +90,8 @@
 	return write, dump;
 end
 
+local event_hooks = {};
+
 local function ringbuffer_log_sink_maker(sink_config)
 	local write, dump = new_buffer(sink_config);
 
@@ -107,8 +109,10 @@
 
 	if sink_config.signal then
 		module:hook_global("signal/"..sink_config.signal, handler);
+		event_hooks[handler] = "signal/"..sink_config.signal;
 	elseif sink_config.event then
 		module:hook_global(sink_config.event, handler);
+		event_hooks[handler] = sink_config.event;
 	end
 
 	return function (name, level, message, ...)
@@ -117,4 +121,11 @@
 	end;
 end
 
+module:hook_global("reopen-log-files", function()
+	for handler, event_name in pairs(event_hooks) do
+		module:unhook_object_event(prosody.events, event_name, handler);
+		event_hooks[handler] = nil;
+	end
+end, 1);
+
 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker);