Skip to content

Commit 3f48a8f

Browse files
committed
log_ratelimit: create flush fiber during log
In the scope of #606 issue we're going to allow disabling the log ratelimiter by setting `LOG_RATELIMIT_INTERVAL` constant to 0. Users, who don't use ratelimiter, should not pay for flush fiber, so for that its creation is moved to the log function instead of creation of the limiter. Closes #606 NO_DOC=internal
1 parent e2c540b commit 3f48a8f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

test/unit-luatest/log_ratelimit_test.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local t = require('luatest')
2+
local fiber = require('fiber')
23
local server = require('test.luatest_helpers.server')
34
local verror = require('vshard.error')
45
local vconsts = require('vshard.consts')
@@ -98,3 +99,36 @@ test_group.test_disable_ratelimit = function()
9899

99100
vconsts.LOG_RATELIMIT_INTERVAL = old_interval
100101
end
102+
103+
test_group.test_fiber_not_started_when_disabled = function()
104+
-- Kill flush fiber from other tests before starting the test.
105+
local function find_fiber_by_name(name)
106+
local fibs = fiber.info()
107+
for id, f in pairs(fibs) do
108+
if f.name == name then
109+
return fiber.find(id)
110+
end
111+
end
112+
end
113+
114+
local flush_fiber_name = 'vshard.ratelimit_flush'
115+
local flush_fiber = find_fiber_by_name(flush_fiber_name)
116+
-- If all other tests are skipped, then it may happen, that the flush fiber
117+
-- doesn't exist at all.
118+
if flush_fiber then
119+
flush_fiber:cancel()
120+
t.helpers.retrying({}, function()
121+
t.assert_not(find_fiber_by_name(flush_fiber_name))
122+
end)
123+
end
124+
vratelimit.internal.flush_fiber = nil
125+
126+
-- Test, that the fiber is not started, if limiter is disabled.
127+
local old_interval = vconsts.LOG_RATELIMIT_INTERVAL
128+
vconsts.LOG_RATELIMIT_INTERVAL = 0
129+
local limiter = vratelimit.create({name = 'test_disable_ratelimit'})
130+
local err = verror.box(box.error.new(box.error.NO_CONNECTION))
131+
limiter:log_error(err)
132+
t.assert_not(find_fiber_by_name(flush_fiber_name))
133+
vconsts.LOG_RATELIMIT_INTERVAL = old_interval
134+
end

vshard/log_ratelimit.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ local function ratelimit_log_template(log_lvl)
124124
-- entries are not printed until it's flushed.
125125
ratelimit_add_entry(limiter, signed_entry)
126126
level = log_lvl
127+
if M.flush_fiber == nil then
128+
M.flush_fiber = util.reloadable_fiber_new(
129+
'vshard.ratelimit_flush', M, 'ratelimit_flush_f')
130+
end
127131
else
128132
-- The entry should be suppressed, it's in the limiter already.
129133
ratelimit_suppress_entry(limiter, signed_entry)
@@ -172,10 +176,6 @@ local function ratelimit_create(cfg)
172176
}
173177
setmetatable(ratelimit, ratelimit_mt)
174178
M.limiters[ratelimit.name] = ratelimit
175-
if M.flush_fiber == nil then
176-
M.flush_fiber = util.reloadable_fiber_new(
177-
'vshard.ratelimit_flush', M, 'ratelimit_flush_f')
178-
end
179179
return ratelimit
180180
end
181181

0 commit comments

Comments
 (0)