This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 725e987 fix: remove unexpected blocking in background timer (#4106)
725e987 is described below
commit 725e9879cdfba39b5a16e027f345ca26ee43c499
Author: 罗泽轩 <[email protected]>
AuthorDate: Fri Apr 23 11:12:46 2021 +0800
fix: remove unexpected blocking in background timer (#4106)
---
apisix/core/timer.lua | 7 +++++--
apisix/timers.lua | 8 ++++++-
t/misc/timers.t | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 3 deletions(-)
diff --git a/apisix/core/timer.lua b/apisix/core/timer.lua
index 2100675..514a5c1 100644
--- a/apisix/core/timer.lua
+++ b/apisix/core/timer.lua
@@ -35,14 +35,17 @@ local function _internal(timer)
local ok, err = pcall(timer.callback_fun)
if not ok then
log.error("failed to run the timer: ", timer.name, " err: ", err)
- sleep(timer.sleep_fail)
+
+ if timer.sleep_fail > 0 then
+ sleep(timer.sleep_fail)
+ end
elseif timer.sleep_succ > 0 then
sleep(timer.sleep_succ)
end
update_time()
- until timer.each_ttl and now() >= timer.start_time + timer.each_ttl
+ until timer.each_ttl <= 0 or now() >= timer.start_time + timer.each_ttl
end
local function run_timer(premature, self)
diff --git a/apisix/timers.lua b/apisix/timers.lua
index b30c269..4c88e2c 100644
--- a/apisix/timers.lua
+++ b/apisix/timers.lua
@@ -21,7 +21,7 @@ local unpack = unpack
local thread_spawn = ngx.thread.spawn
local thread_wait = ngx.thread.wait
-local check_interval = 0.5
+local check_interval = 1
local timers = {}
@@ -30,6 +30,10 @@ local _M = {}
local function background_timer()
+ if core.table.nkeys(timers) == 0 then
+ return
+ end
+
local threads = {}
for name, timer in pairs(timers) do
core.log.info("run timer[", name, "]")
@@ -59,6 +63,8 @@ end
function _M.init_worker()
local opts = {
+ each_ttl = 0,
+ sleep_succ = 0,
check_interval = check_interval,
}
local timer, err = core.timer.new("background", background_timer, opts)
diff --git a/t/misc/timers.t b/t/misc/timers.t
new file mode 100644
index 0000000..2fed6cc
--- /dev/null
+++ b/t/misc/timers.t
@@ -0,0 +1,58 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!defined $block->error_log && !defined $block->no_error_log) {
+ $block->set_value("no_error_log", "[error]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: always start a new timer even the previous one is blocked
+--- config
+ location /t {
+ content_by_lua_block {
+ local timers = require("apisix.timers")
+ timers.register_timer("t", function()
+ ngx.log(ngx.WARN, "fire")
+ end)
+ timers.register_timer("c", function()
+ ngx.sleep(5)
+ end)
+
+ ngx.sleep(2.1)
+ }
+ }
+--- grep_error_log eval
+qr/fire/
+--- grep_error_log_out
+fire
+fire