view mod_measure_storage/mod_measure_storage.lua @ 3656:3e0f4d727825

mod_vcard_muc: Add an alternative method of signaling avatar change When the avatar has been changed, a signal is sent that the room configuration has changed. Clients then do a disco#info query to find the SHA-1 of the new avatar. They can then fetch it as before, or not if they have it cached already. This is meant to be less disruptive than signaling via presence, which caused problems for some clients. If clients transition to the new method, the old one can eventually be removed. The namespace is made up while waiting for standardization. Otherwise it is very close to what's described in https://xmpp.org/extensions/inbox/muc-avatars.html
author Kim Alvefur <zash@zash.se>
date Sun, 25 Aug 2019 20:46:43 +0200
parents 04ae5b45e6c7
children
line wrap: on
line source

module:set_global()

local function return_args_after_calling(f, ...)
	f();
	return ...
end

local function time_method(module, store_name, store_type, method_name, method_function)
	local opt_use_tags = module:get_option_boolean("measure_storage_tagged_metric", false);

	local metric_name, metric_tags;
	if opt_use_tags then
		metric_name, metric_tags = "storage_operation", ("store_name:%s,store_type:%s,store_operation:%s"):format(store_name, store_type, method_name);
	else
		metric_name = store_name.."_"..store_type.."_"..method_name;
	end
	local measure_operation_started = module:measure(metric_name, "times", metric_tags);

	return function (...)
		module:log("debug", "Measuring storage operation %s (%s)", metric_name, metric_tags or "no tags");
		local measure_operation_complete = measure_operation_started();
		return return_args_after_calling(measure_operation_complete, method_function(...));
	end;
end

local function wrap_store(module, store_name, store_type, store)
	local new_store = setmetatable({}, {
		__index = function (t, method_name)
			local original_method = store[method_name];
			if type(original_method) ~= "function" then
				if original_method then
					rawset(t, method_name, original_method);
				end
				return original_method;
			end
			local timed_method = time_method(module, store_name, store_type, method_name, original_method);
			rawset(t, method_name, timed_method);
			return timed_method;
		end;
	});
	return new_store;
end

local function hook_event(module)
	module:hook("store-opened", function(event)
		event.store = wrap_store(module, event.store_name, event.store_type or "keyval", event.store);
	end);
end

function module.load()
	hook_event(module);
end

function module.add_host(module)
	hook_event(module);
end