Comparison

plugins/mod_limits.lua @ 8269:25237002aba4

mod_limits: Handle fractional outstanding balance values (caused by e3f7b6fa46ba) Fractional values were passed to string.sub() when doing buffer manipulations, and caused random bytes to be skipped in the stream.
author Matthew Wild <mwild1@gmail.com>
date Tue, 26 Sep 2017 17:48:33 +0100
parent 8256:cdffe33efae4
child 8453:6b3e7fddd723
comparison
equal deleted inserted replaced
8267:42fad8465537 8269:25237002aba4
2 module:set_global(); 2 module:set_global();
3 3
4 local filters = require "util.filters"; 4 local filters = require "util.filters";
5 local throttle = require "util.throttle"; 5 local throttle = require "util.throttle";
6 local timer = require "util.timer"; 6 local timer = require "util.timer";
7 local ceil = math.ceil;
7 8
8 local limits_cfg = module:get_option("limits", {}); 9 local limits_cfg = module:get_option("limits", {});
9 local limits_resolution = module:get_option_number("limits_resolution", 1); 10 local limits_resolution = module:get_option_number("limits_resolution", 1);
10 11
11 local default_bytes_per_second = 3000; 12 local default_bytes_per_second = 3000;
53 local throttle = session.throttle; 54 local throttle = session.throttle;
54 if throttle then 55 if throttle then
55 local ok, balance, outstanding = throttle:poll(#bytes, true); 56 local ok, balance, outstanding = throttle:poll(#bytes, true);
56 if not ok then 57 if not ok then
57 session.log("debug", "Session over rate limit (%d) with %d (by %d), pausing", throttle.max, #bytes, outstanding); 58 session.log("debug", "Session over rate limit (%d) with %d (by %d), pausing", throttle.max, #bytes, outstanding);
59 outstanding = ceil(outstanding);
58 session.conn:pause(); -- Read no more data from the connection until there is no outstanding data 60 session.conn:pause(); -- Read no more data from the connection until there is no outstanding data
59 local outstanding_data = bytes:sub(-outstanding); 61 local outstanding_data = bytes:sub(-outstanding);
60 bytes = bytes:sub(1, #bytes-outstanding); 62 bytes = bytes:sub(1, #bytes-outstanding);
61 timer.add_task(limits_resolution, function () 63 timer.add_task(limits_resolution, function ()
62 if not session.conn then return; end 64 if not session.conn then return; end