comparison mod_push2/mod_push2.lua @ 6337:0570f1e2edf4

mod_push2: Implement cache for user push registrations
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Oct 2025 14:17:11 +0100
parents 2488254c1bc2
children c9836434a9d5
comparison
equal deleted inserted replaced
6336:c819b1b5a775 6337:0570f1e2edf4
18 local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*3600) -- use same timeout like ejabberd 18 local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*3600) -- use same timeout like ejabberd
19 local hibernate_past_first_push = module:get_option_boolean("hibernate_past_first_push", true) 19 local hibernate_past_first_push = module:get_option_boolean("hibernate_past_first_push", true)
20 20
21 local host_sessions = prosody.hosts[module.host].sessions 21 local host_sessions = prosody.hosts[module.host].sessions
22 local push2_registrations = module:open_store("push2_registrations", "keyval") 22 local push2_registrations = module:open_store("push2_registrations", "keyval")
23 local push2_registrations_cache = require "prosody.util.cache".new(10);
23 24
24 if _VERSION:match("5%.1") or _VERSION:match("5%.2") then 25 if _VERSION:match("5%.1") or _VERSION:match("5%.2") then
25 module:log("warn", "This module may behave incorrectly on Lua before 5.3. It is recommended to upgrade to a newer Lua version.") 26 module:log("warn", "This module may behave incorrectly on Lua before 5.3. It is recommended to upgrade to a newer Lua version.")
26 end 27 end
27 28
112 local registrations = push2_registrations:get(origin.username) or {} 113 local registrations = push2_registrations:get(origin.username) or {}
113 registrations[registration_id] = push_registration 114 registrations[registration_id] = push_registration
114 if not push2_registrations:set(origin.username, registrations) then 115 if not push2_registrations:set(origin.username, registrations) then
115 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); 116 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
116 else 117 else
118 push2_registrations_cache:set(origin.username, registrations);
117 origin.push_registration_id = registration_id 119 origin.push_registration_id = registration_id
118 origin.push_registration = push_registration 120 origin.push_registration = push_registration
119 origin.first_hibernated_push = nil 121 origin.first_hibernated_push = nil
120 origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(service_jid)) 122 origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(service_jid))
121 origin.send(st.reply(stanza)) 123 origin.send(st.reply(stanza))
134 local registrations = push2_registrations:get(origin.username) or {} 136 local registrations = push2_registrations:get(origin.username) or {}
135 registrations[registration_id] = nil 137 registrations[registration_id] = nil
136 if not push2_registrations:set(origin.username, registrations) then 138 if not push2_registrations:set(origin.username, registrations) then
137 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); 139 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
138 else 140 else
141 push2_registrations_cache:set(origin.username, nil);
139 origin.push_registration_id = nil 142 origin.push_registration_id = nil
140 origin.push_registration = nil 143 origin.push_registration = nil
141 origin.first_hibernated_push = nil 144 origin.first_hibernated_push = nil
142 origin.log("info", "Push notifications disabled for %s (%s)", tostring(stanza.attr.from), registration_id) 145 origin.log("info", "Push notifications disabled for %s (%s)", tostring(stanza.attr.from), registration_id)
143 origin.send(st.reply(stanza)) 146 origin.send(st.reply(stanza))
444 447
445 -- small helper function to extract relevant push settings 448 -- small helper function to extract relevant push settings
446 local function get_push_settings(stanza, session) 449 local function get_push_settings(stanza, session)
447 local to = stanza.attr.to 450 local to = stanza.attr.to
448 local node = to and jid.split(to) or session.username 451 local node = to and jid.split(to) or session.username
449 local user_push_services = push2_registrations:get(node) 452 local user_push_services = push2_registrations_cache:get(node);
453 if not user_push_services then
454 user_push_services = push2_registrations:get(node);
455 push2_registrations_cache:set(node, user_push_services);
456 end
450 return node, (user_push_services or {}) 457 return node, (user_push_services or {})
451 end 458 end
452 459
453 -- publish on offline message 460 -- publish on offline message
454 module:hook("message/offline/handle", function(event) 461 module:hook("message/offline/handle", function(event)