Browse Source

[Fix] Fix ratelimit initialization

tags/1.3.0
Vsevolod Stakhov 8 years ago
parent
commit
59e81754e6
1 changed files with 10 additions and 7 deletions
  1. 10
    7
      src/plugins/lua/ratelimit.lua

+ 10
- 7
src/plugins/lua/ratelimit.lua View File

@@ -108,7 +108,7 @@ local function parse_limits(data)
else
return {0, 0, 0}
end
end):totable()
end):totable()
end

local function generate_format_string(args, is_set)
@@ -129,8 +129,7 @@ local function check_limits(task, args)
--- Called when value is got from server
local function rate_get_cb(task, err, data)
if data then
local tv = task:get_timeval()
local ntime = tv['tv_usec'] / 1000000. + tv['tv_sec']
local ntime = rspamd_util.get_time()

_.each(function(elt, limit)
local bucket = elt[2]
@@ -139,6 +138,8 @@ local function check_limits(task, args)
local atime = elt[1]
local ctime = elt[3]

if atime == 0 then return end

if atime - ctime > max_delay then
rspamd_logger.infox(task, 'limit is too old: %1 seconds; ignore it',
atime - ctime)
@@ -182,8 +183,7 @@ local function set_limits(task, args)

local function rate_set_cb(task, err, data)
if data then
local tv = task:get_timeval()
local ntime = tv['tv_usec'] / 1000000. + tv['tv_sec']
local ntime = rspamd_util.get_time()
local values = {}
_.each(function(elt, limit)
local bucket = elt[2]
@@ -193,10 +193,11 @@ local function set_limits(task, args)
local ctime = elt[3]

if atime - ctime > max_delay then
bucket = 1
ctime = atime
rspamd_logger.infox(task, 'limit is too old: %1 seconds; start it over',
atime - ctime)
bucket = 1
ctime = ntime
atime = ntime
else
if bucket > 0 then
bucket = bucket - rate * (ntime - atime) + 1;
@@ -208,6 +209,8 @@ local function set_limits(task, args)
end
end

if ctime == 0 then ctime = ntime end

local lstr = string.format('%.3f:%.3f:%.3f', ntime, bucket, ctime)
table.insert(values, {limit[2], max_delay, lstr})
end, _.zip(parse_limits(data), _.iter(args)))

Loading…
Cancel
Save