comparison mod_muc_limits/mod_muc_limits.lua @ 3098:a81456a13797

mod_client_proxy: a Jabber Address Translation implementation
author Jonas Wielicki <jonas@wielicki.name>
date Sun, 03 Jun 2018 13:55:10 +0200
parents 163967467308
children 5c3f3f5a4377
comparison
equal deleted inserted replaced
3097:c7f4e3987ed0 3098:a81456a13797
23 module:log("warn", "Dropped %d stanzas from %d JIDs: %s", dropped_count, #dropped_jids, t_concat(dropped_jids, ", ")); 23 module:log("warn", "Dropped %d stanzas from %d JIDs: %s", dropped_count, #dropped_jids, t_concat(dropped_jids, ", "));
24 dropped_count = 0; 24 dropped_count = 0;
25 dropped_jids = nil; 25 dropped_jids = nil;
26 end 26 end
27 27
28 local function get_non_outcast_affiliations(room)
29 local nmembers = 0;
30 -- this is an evil hack, we probably should not access _affiliations
31 -- directly ...
32 for _, aff in pairs(room._affiliations) do
33 if aff ~= "outcast" then
34 nmembers = nmembers + 1;
35 end
36 end
37 return nmembers;
38 end
39
28 local function handle_stanza(event) 40 local function handle_stanza(event)
29 local origin, stanza = event.origin, event.stanza; 41 local origin, stanza = event.origin, event.stanza;
30 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving 42 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving
31 return; 43 return;
32 end 44 end
47 local throttle = room.throttle; 59 local throttle = room.throttle;
48 if not room.throttle then 60 if not room.throttle then
49 throttle = new_throttle(period*burst, burst); 61 throttle = new_throttle(period*burst, burst);
50 room.throttle = throttle; 62 room.throttle = throttle;
51 end 63 end
52 if not throttle:poll(1) then 64
65 local cost = 1;
66 -- we scale the cost by the inverse of the square root of the number of
67 -- members; this should effectively raise the limit by a factor of
68 -- sqrt(nmembers)
69 local nmembers = math.max(get_non_outcast_affiliations(room), 1);
70 cost = cost / math.sqrt(nmembers);
71
72 if not throttle:poll(cost) then
53 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); 73 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
54 if not dropped_jids then 74 if not dropped_jids then
55 dropped_jids = { [from_jid] = true, from_jid }; 75 dropped_jids = { [from_jid] = true, from_jid };
56 module:add_timer(5, log_dropped); 76 module:add_timer(5, log_dropped);
57 elseif not dropped_jids[from_jid] then 77 elseif not dropped_jids[from_jid] then