Software / code / prosody-modules
Comparison
mod_presence_cache/mod_presence_cache.lua @ 3183:b718092e442f
mod_presence_cache: Forget cached presence on s2s close
This should remove stale entries from unclean shutdowns and similar.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 13 Jul 2018 00:47:29 +0200 |
| parent | 2292:54f44365a378 |
| child | 3373:322e8e7ba7d4 |
comparison
equal
deleted
inserted
replaced
| 3182:9e5616a49d59 | 3183:b718092e442f |
|---|---|
| 4 -- This file is MIT/X11 licensed. | 4 -- This file is MIT/X11 licensed. |
| 5 | 5 |
| 6 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed; | 6 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed; |
| 7 local jid_split = require"util.jid".split; | 7 local jid_split = require"util.jid".split; |
| 8 local jid_bare = require"util.jid".bare; | 8 local jid_bare = require"util.jid".bare; |
| 9 local jid_host = require"util.jid".host; | |
| 9 local st = require"util.stanza"; | 10 local st = require"util.stanza"; |
| 10 local datetime = require"util.datetime"; | 11 local datetime = require"util.datetime"; |
| 11 local cache = require "util.cache"; | 12 local cache = require "util.cache"; |
| 12 | 13 |
| 13 local cache_size = module:get_option_number("presence_cache_size", 100); | 14 local cache_size = module:get_option_number("presence_cache_size", 100); |
| 96 origin.send(presence); | 97 origin.send(presence); |
| 97 end | 98 end |
| 98 end | 99 end |
| 99 | 100 |
| 100 module:hook("pre-presence/bare", answer_probe_from_cache, 10); | 101 module:hook("pre-presence/bare", answer_probe_from_cache, 10); |
| 102 | |
| 103 local function clear_cache_from_s2s(remote, reason) | |
| 104 if not remote then return end | |
| 105 if reason and reason:find("timeout") then return end -- Ignore connections closed for being idle | |
| 106 | |
| 107 module:log("debug", "Dropping cached presence from host %s", remote); | |
| 108 | |
| 109 for bare, cached in pairs(bare_cache) do | |
| 110 if jid_host(bare) == remote then | |
| 111 for jid in pairs(cached) do | |
| 112 presence_cache:set(jid, nil); | |
| 113 end | |
| 114 bare_cache[bare] = nil; | |
| 115 end | |
| 116 end | |
| 117 end | |
| 118 | |
| 119 module:hook("s2sin-destroyed", function (event) | |
| 120 return clear_cache_from_s2s(event.session.from_host, event.reason); | |
| 121 end); | |
| 122 | |
| 123 module:hook("s2sout-destroyed", function (event) | |
| 124 return clear_cache_from_s2s(event.session.to_host, event.reason); | |
| 125 end); |