comparison mod_limits/mod_limits.lua @ 2777:55a7ef2fb628

mod_limits: Handle fractional outstanding balance (imported from prosody 25237002aba4)
author Matthew Wild <mwild1@gmail.com>
date Tue, 26 Sep 2017 17:51:09 +0100
parents 1c126c49f5c1
children 88b16084eda7
comparison
equal deleted inserted replaced
2776:3092ae96c1f0 2777:55a7ef2fb628
6 module:set_global(); 6 module:set_global();
7 7
8 local filters = require "util.filters"; 8 local filters = require "util.filters";
9 local throttle = require "util.throttle"; 9 local throttle = require "util.throttle";
10 local timer = require "util.timer"; 10 local timer = require "util.timer";
11 local ceil = math.ceil;
11 12
12 local limits_cfg = module:get_option("limits", {}); 13 local limits_cfg = module:get_option("limits", {});
13 local limits_resolution = module:get_option_number("limits_resolution", 1); 14 local limits_resolution = module:get_option_number("limits_resolution", 1);
14 15
15 local default_bytes_per_second = 3000; 16 local default_bytes_per_second = 3000;
57 local throttle = session.throttle; 58 local throttle = session.throttle;
58 if throttle then 59 if throttle then
59 local ok, balance, outstanding = throttle:poll(#bytes, true); 60 local ok, balance, outstanding = throttle:poll(#bytes, true);
60 if not ok then 61 if not ok then
61 session.log("debug", "Session over rate limit (%d) with %d (by %d), pausing", throttle.max, #bytes, outstanding); 62 session.log("debug", "Session over rate limit (%d) with %d (by %d), pausing", throttle.max, #bytes, outstanding);
63 outstanding = ceil(outstanding);
62 session.conn:pause(); -- Read no more data from the connection until there is no outstanding data 64 session.conn:pause(); -- Read no more data from the connection until there is no outstanding data
63 local outstanding_data = bytes:sub(-outstanding); 65 local outstanding_data = bytes:sub(-outstanding);
64 bytes = bytes:sub(1, #bytes-outstanding); 66 bytes = bytes:sub(1, #bytes-outstanding);
65 timer.add_task(limits_resolution, function () 67 timer.add_task(limits_resolution, function ()
66 if not session.conn then return; end 68 if not session.conn then return; end