Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_timer option can work in non debug mode #68

Closed
wants to merge 16 commits into from
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ env:
- OPENSSL_PREFIX=/opt/ssl
- OPENSSL_LIB=$OPENSSL_PREFIX/lib
- OPENSSL_INC=$OPENSSL_PREFIX/include
- OPENSSL_VER=1.0.2j
- OPENSSL_VER=1.1.1f
- LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH
- TEST_NGINX_SLEEP=0.006
matrix:
Expand Down
19 changes: 11 additions & 8 deletions lib/resty/upstream/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,16 @@ check = function (premature, ctx)
errlog("failed to run healthcheck cycle: ", err)
end

local ok, err = new_timer(ctx.interval, check, ctx)
if not ok then
if err ~= "process exiting" then
errlog("failed to create timer: ", err)
end
if not ctx.no_timer then
local ok, err = new_timer(ctx.interval, check, ctx)
if not ok then
if err ~= "process exiting" then
errlog("failed to create timer: ", err)
end

update_upstream_checker_status(ctx.upstream, false)
return
update_upstream_checker_status(ctx.upstream, false)
return
end
end
end

Expand Down Expand Up @@ -618,9 +620,10 @@ function _M.spawn_checker(opts)
statuses = statuses,
version = 0,
concurrency = concur,
no_timer = opts.no_timer,
}

if debug_mode and opts.no_timer then
if opts.no_timer then
check(nil, ctx)

else
Expand Down
81 changes: 80 additions & 1 deletion t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Cwd qw(cwd);

#repeat_each(2);

plan tests => repeat_each() * (blocks() * 6 + 9);
plan tests => repeat_each() * (blocks() * 6 + 5);

my $pwd = cwd();

Expand Down Expand Up @@ -1451,3 +1451,82 @@ healthcheck: peer 127\.0\.0\.1:12355 was checked to be not ok
healthcheck: peer 127\.0\.0\.1:12356 was checked to be not ok
healthcheck: peer 127\.0\.0\.1:12359 was checked to be not ok
$/

=== TEST 16: effect of no_timer (healthcheck options can be changed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: we need three blank lines before this line. you are recommended to use the reindex tool to do it automatically:
https://github.com/openresty/openresty-devel-utils/blob/master/reindex

doc is still in the PR: https://github.com/openresty/openresty-devel-utils/pull/29/files#diff-04c6e90faac2675aa89e2176d2eec7d8R237

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

--- http_config eval
"$::HttpConfig"
. q{
upstream foo.com {
server 127.0.0.1:12354;
}

server {
listen 12354;

location = /status {
content_by_lua_block {
ngx.say("ok")
}
}
}

lua_shared_dict healthcheck 1m;
}
--- config
location = /t {
content_by_lua_block {
local hc = require "resty.upstream.healthcheck"
for i = 1,2,1 do
local ok, err = hc.spawn_checker{
shm = "healthcheck",
upstream = "foo.com",
type = "http",
http_req = "GET /status_not_exist HTTP/1.0\r\nHost: localhost\r\n\r\n",
timeout = 100,
fall = 1,
concurrency = 1,
valid_statuses = {200},
no_timer = true,
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
ngx.sleep(0.5)
end
ngx.say(hc.status_page())

for i = 1,3,1 do
local ok, err = hc.spawn_checker{
shm = "healthcheck",
upstream = "foo.com",
type = "http",
http_req = "GET /status HTTP/1.0\r\nHost: localhost\r\n\r\n",
timeout = 100,
fall = 1,
concurrency = 1,
valid_statuses = {200},
no_timer = true,
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
ngx.sleep(0.5)
end
ngx.say(hc.status_page())
}
}
--- request
GET /t
--- response_body_like
Upstream foo.com
Primary Peers
127.0.0.1:12354 DOWN
Backup Peers

Upstream foo.com
Primary Peers
127.0.0.1:12354 up
Backup Peers
--- timeout: 5