mod_limits: Use rate limiting in net.server if provided
authorKim Alvefur <zash@zash.se>
Fri, 29 Mar 2019 00:58:22 +0100
changeset 10103 7e3196e0263e
parent 10102 dda1c7ccb209
child 10104 5ca9c4917885
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.
plugins/mod_limits.lua
--- a/plugins/mod_limits.lua	Sun Dec 18 17:39:16 2016 +0000
+++ b/plugins/mod_limits.lua	Fri Mar 29 00:58:22 2019 +0100
@@ -84,8 +84,13 @@
 	local session_type = session.type:match("^[^_]+");
 	local filter_set, opts = type_filters[session_type], limits[session_type];
 	if opts then
-		session.throttle = throttle.create(opts.bytes_per_second * opts.burst_seconds, opts.burst_seconds);
-		filters.add_filter(session, "bytes/in", filter_set.bytes_in, 1000);
+		if session.conn and session.conn.setlimit then
+			session.conn:setlimit(opts.bytes_per_second);
+			-- Currently no burst support
+		else
+			session.throttle = throttle.create(opts.bytes_per_second * opts.burst_seconds, opts.burst_seconds);
+			filters.add_filter(session, "bytes/in", filter_set.bytes_in, 1000);
+		end
 	end
 end
 
@@ -106,9 +111,14 @@
 			local session_type = session.type:match("^[^_]+");
 			local jid = session.username .. "@" .. session.host;
 			if unlimited_jids:contains(jid) then
-				local filter_set = type_filters[session_type];
-				filters.remove_filter(session, "bytes/in", filter_set.bytes_in);
-				session.throttle = nil;
+				if session.conn and session.conn.setlimit then
+					session.conn:setlimit(0);
+					-- Currently no burst support
+				else
+					local filter_set = type_filters[session_type];
+					filters.remove_filter(session, "bytes/in", filter_set.bytes_in);
+					session.throttle = nil;
+				end
 			end
 		end);
 	end