Comparison

plugins/mod_limits.lua @ 10099:7e3196e0263e

mod_limits: Use rate limiting in net.server if provided This should be simpler and more efficient, as well avoid problems caused by using filters.
author Kim Alvefur <zash@zash.se>
date Fri, 29 Mar 2019 00:58:22 +0100
parent 9943:46773fe2be45
child 10111:0f335815244f
comparison
equal deleted inserted replaced
10098:dda1c7ccb209 10099:7e3196e0263e
82 82
83 local function filter_hook(session) 83 local function filter_hook(session)
84 local session_type = session.type:match("^[^_]+"); 84 local session_type = session.type:match("^[^_]+");
85 local filter_set, opts = type_filters[session_type], limits[session_type]; 85 local filter_set, opts = type_filters[session_type], limits[session_type];
86 if opts then 86 if opts then
87 session.throttle = throttle.create(opts.bytes_per_second * opts.burst_seconds, opts.burst_seconds); 87 if session.conn and session.conn.setlimit then
88 filters.add_filter(session, "bytes/in", filter_set.bytes_in, 1000); 88 session.conn:setlimit(opts.bytes_per_second);
89 -- Currently no burst support
90 else
91 session.throttle = throttle.create(opts.bytes_per_second * opts.burst_seconds, opts.burst_seconds);
92 filters.add_filter(session, "bytes/in", filter_set.bytes_in, 1000);
93 end
89 end 94 end
90 end 95 end
91 96
92 function module.load() 97 function module.load()
93 filters.add_filter_hook(filter_hook); 98 filters.add_filter_hook(filter_hook);
104 module:hook("authentication-success", function (event) 109 module:hook("authentication-success", function (event)
105 local session = event.session; 110 local session = event.session;
106 local session_type = session.type:match("^[^_]+"); 111 local session_type = session.type:match("^[^_]+");
107 local jid = session.username .. "@" .. session.host; 112 local jid = session.username .. "@" .. session.host;
108 if unlimited_jids:contains(jid) then 113 if unlimited_jids:contains(jid) then
109 local filter_set = type_filters[session_type]; 114 if session.conn and session.conn.setlimit then
110 filters.remove_filter(session, "bytes/in", filter_set.bytes_in); 115 session.conn:setlimit(0);
111 session.throttle = nil; 116 -- Currently no burst support
117 else
118 local filter_set = type_filters[session_type];
119 filters.remove_filter(session, "bytes/in", filter_set.bytes_in);
120 session.throttle = nil;
121 end
112 end 122 end
113 end); 123 end);
114 end 124 end
115 end 125 end