Annotate

mod_presence_cache/mod_presence_cache.lua @ 2175:cc0cf6748885

mod_presence_cache: Also cache the 'show' value
author Kim Alvefur <zash@zash.se>
date Wed, 11 May 2016 23:12:51 +0200
parent 2153:aa24d49c47ef
child 2176:b76b062e77db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid_split = require"util.jid".split;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_bare = require"util.jid".bare;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local st = require"util.stanza";
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local datetime = require"util.datetime";
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
6 local cache = require "util.cache";
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
8 local cache_size = module:get_option_number("presence_cache_size", 100);
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
9
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
10 local bare_cache = {}; -- [username NUL bare_jid] = { [full_jid] = { timestamp, ... } }
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
11
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
12 local function on_evict(cache_key)
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
13 local bare_cache_key = cache_key:match("^%Z+%z[^/]+");
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
14 local full_jid = cache_key:match("%z(.*)$");
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
15 local jids = bare_cache[bare_cache_key];
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
16
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
17 if jids then
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
18 jids[full_jid] = nil;
2153
aa24d49c47ef mod_presence_cache: Only check set set is empty if it exists
Kim Alvefur <zash@zash.se>
parents: 2152
diff changeset
19 if next(jids) == nil then
aa24d49c47ef mod_presence_cache: Only check set set is empty if it exists
Kim Alvefur <zash@zash.se>
parents: 2152
diff changeset
20 bare_cache[bare_cache_key] = nil;
aa24d49c47ef mod_presence_cache: Only check set set is empty if it exists
Kim Alvefur <zash@zash.se>
parents: 2152
diff changeset
21 end
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
22 end
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
23 end
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
24
2152
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
25 -- used indirectly for the on_evict callback
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
26 local presence_cache = cache.new(cache_size, on_evict);
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local function cache_hook(event)
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local origin, stanza = event.origin, event.stanza;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local typ = stanza.attr.type;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 module:log("debug", "Cache hook, got %s from a %s", stanza:top_tag(), origin.type);
2145
f965f86a5cad mod_presence_cache: Check stanzas only from s2sin (not needed, even with mod_bidi)
Kim Alvefur <zash@zash.se>
parents: 2123
diff changeset
32 if origin.type == "s2sin" and ( typ == nil or typ == "unavailable" ) then
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
34 local contact_full = stanza.attr.from;
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
35 local contact_bare = jid_bare(contact_full);
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
36 local username, host = jid_split(stanza.attr.to);
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
37
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
38 if not is_contact_subscribed(username, host, contact_bare) then
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
39 module:log("debug", "Presence from jid not in roster");
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
43 local cache_key = username .. "\0" .. contact_full;
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
44 local bare_cache_key = username .. "\0" .. contact_bare;
2152
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
45
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
46 local jids = bare_cache[bare_cache_key];
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
47
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
48 if typ == "unavailable" then -- remove from cache
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
49 presence_cache:set(cache_key, nil);
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
50 on_evict(cache_key);
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
51 return;
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
52 end
bb4a2e4b7ba7 mod_presence_cache: Forget about cached presence when receiving unavailable
Kim Alvefur <zash@zash.se>
parents: 2147
diff changeset
53
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
54 local presence_bits = {
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
55 stamp = datetime.datetime();
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
56 show = stanza:get_child_text("show");
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
57 };
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
58 if jids then
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
59 jids[contact_full] = presence_bits;
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
60 else
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
61 jids = { [contact_full] = presence_bits };
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
62 bare_cache[bare_cache_key] = jids;
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
64 presence_cache:set(cache_key, true);
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 module:hook("presence/bare", cache_hook, 10);
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 -- module:hook("presence/full", cache_hook, 10);
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local function answer_probe_from_cache(event)
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 local origin, stanza = event.origin, event.stanza;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if stanza.attr.type ~= "probe" then return; end
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
74
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
75 local username = origin.username;
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local contact_bare = stanza.attr.to;
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
78 local bare_cache_key = username .. "\0" .. contact_bare;
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
80 local cached = bare_cache[bare_cache_key];
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
81 if not cached then return end
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
82 for jid, presence_bits in pairs(cached) do
2147
ed2bb50d4f91 mod_presence_cache: Switch to using util.cache for limiting size of cache
Kim Alvefur <zash@zash.se>
parents: 2146
diff changeset
83 local presence = st.presence({ to = origin.full_jid, from = jid })
2175
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
84 if presence_bits.show then
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
85 presence:tag("show"):text(presence_bits.show):up();
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
86 end
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
87 if presence_bits.stamp then
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
88 presence:tag("delay", { xmlns = "urn:xmpp:delay", from = module.host, stamp = presence_bits.stamp }):up();
cc0cf6748885 mod_presence_cache: Also cache the 'show' value
Kim Alvefur <zash@zash.se>
parents: 2153
diff changeset
89 end
1952
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 origin.send(presence);
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93
9d0c33ebbcc5 mod_presence_cache: Cache incoming presence broadcasts in order to get clients up to speed with who is online faster
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 module:hook("pre-presence/bare", answer_probe_from_cache, 10);