Skip to content

Commit d775dd8

Browse files
hishamhmlocao
andcommitted
feat(healthcheck) set_all_target_statuses_for_hostname
Co-Authored-By: Vinicius Mignot <vinicius.mignot@gmail.com>
1 parent 028d2bf commit d775dd8

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

lib/resty/healthcheck.lua

+26
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,32 @@ function checker:report_timeout(ip, port, hostname, check)
711711
end
712712

713713

714+
--- Sets the current status of all targets with the given hostname and port.
715+
-- @param hostname hostname being checked.
716+
-- @param port the port being checked against
717+
-- @param is_healthy boolean: `true` for healthy, `false` for unhealthy
718+
-- @return `true` on success, or `nil + error` on failure.
719+
function checker:set_all_target_statuses_for_hostname(hostname, port, is_healthy)
720+
assert(type(hostname) == "string", "no hostname provided")
721+
port = assert(tonumber(port), "no port number provided")
722+
assert(type(is_healthy) == "boolean")
723+
724+
local all_ok = true
725+
local errs = {}
726+
for _, target in ipairs(self.targets) do
727+
if target.port == port and target.hostname == hostname then
728+
local ok, err = self:set_target_status(target.ip, port, hostname, is_healthy)
729+
if not ok then
730+
all_ok = nil
731+
table.insert(errs, err)
732+
end
733+
end
734+
end
735+
736+
return all_ok, #errs > 0 and table.concat(errs, "; ") or nil
737+
end
738+
739+
714740
--- Sets the current status of the target.
715741
-- This will immediately set the status and clear its counters.
716742
-- @param ip IP address of the target being checked
+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
use Test::Nginx::Socket::Lua;
2+
use Cwd qw(cwd);
3+
4+
workers(1);
5+
6+
plan tests => repeat_each() * blocks() * 2;
7+
8+
my $pwd = cwd();
9+
10+
our $HttpConfig = qq{
11+
lua_package_path "$pwd/lib/?.lua;;";
12+
lua_shared_dict test_shm 8m;
13+
lua_shared_dict my_worker_events 8m;
14+
};
15+
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: set_all_target_statuses_for_hostname() updates statuses
21+
--- http_config eval
22+
qq{
23+
$::HttpConfig
24+
}
25+
--- config
26+
location = /t {
27+
content_by_lua_block {
28+
local we = require "resty.worker.events"
29+
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
30+
local healthcheck = require("resty.healthcheck")
31+
local checker = healthcheck.new({
32+
name = "testing",
33+
shm_name = "test_shm",
34+
})
35+
ngx.sleep(0.1) -- wait for initial timers to run once
36+
checker:add_target("127.0.0.1", 2112, "rush", true)
37+
checker:add_target("127.0.0.2", 2112, "rush", true)
38+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
39+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
40+
checker:set_all_target_statuses_for_hostname("rush", 2112, false)
41+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false
42+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false
43+
checker:set_all_target_statuses_for_hostname("rush", 2112, true)
44+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
45+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
46+
}
47+
}
48+
--- request
49+
GET /t
50+
--- response_body
51+
true
52+
true
53+
false
54+
false
55+
true
56+
true
57+
=== TEST 2: set_all_target_statuses_for_hostname() restores node after passive check disables it
58+
--- http_config eval
59+
qq{
60+
$::HttpConfig
61+
}
62+
--- config
63+
location = /t {
64+
content_by_lua_block {
65+
local we = require "resty.worker.events"
66+
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
67+
local healthcheck = require("resty.healthcheck")
68+
local checker = healthcheck.new({
69+
name = "testing",
70+
shm_name = "test_shm",
71+
checks = {
72+
passive = {
73+
unhealthy = {
74+
tcp_failures = 2,
75+
http_failures = 2,
76+
}
77+
}
78+
}
79+
})
80+
ngx.sleep(0.1) -- wait for initial timers to run once
81+
checker:add_target("127.0.0.1", 2112, "rush", true)
82+
checker:add_target("127.0.0.2", 2112, "rush", true)
83+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
84+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
85+
checker:report_http_status("127.0.0.1", 2112, "rush", 500)
86+
checker:report_http_status("127.0.0.1", 2112, "rush", 500)
87+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false
88+
checker:set_all_target_statuses_for_hostname("rush", 2112, true)
89+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
90+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
91+
}
92+
}
93+
--- request
94+
GET /t
95+
--- response_body
96+
true
97+
true
98+
false
99+
true
100+
true
101+
=== TEST 3: set_all_target_statuses_for_hostname() resets failure counters
102+
--- http_config eval
103+
qq{
104+
$::HttpConfig
105+
}
106+
--- config
107+
location = /t {
108+
content_by_lua_block {
109+
local we = require "resty.worker.events"
110+
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
111+
local healthcheck = require("resty.healthcheck")
112+
local checker = healthcheck.new({
113+
name = "testing",
114+
shm_name = "test_shm",
115+
checks = {
116+
passive = {
117+
healthy = {
118+
successes = 2,
119+
},
120+
unhealthy = {
121+
tcp_failures = 2,
122+
http_failures = 2,
123+
}
124+
}
125+
}
126+
})
127+
ngx.sleep(0.1) -- wait for initial timers to run once
128+
checker:add_target("127.0.0.1", 2112, "rush", true)
129+
checker:add_target("127.0.0.2", 2112, "rush", true)
130+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
131+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
132+
checker:report_http_status("127.0.0.1", 2112, "rush", 500)
133+
checker:set_all_target_statuses_for_hostname("rush", 2112, true)
134+
checker:report_http_status("127.0.0.1", 2112, "rush", 500)
135+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
136+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
137+
checker:report_http_status("127.0.0.1", 2112, "rush", 500)
138+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false
139+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true
140+
}
141+
}
142+
--- request
143+
GET /t
144+
--- response_body
145+
true
146+
true
147+
true
148+
true
149+
false
150+
true
151+
=== TEST 4: set_target_status() resets the success counters
152+
--- http_config eval
153+
qq{
154+
$::HttpConfig
155+
}
156+
--- config
157+
location = /t {
158+
content_by_lua_block {
159+
local we = require "resty.worker.events"
160+
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
161+
local healthcheck = require("resty.healthcheck")
162+
local checker = healthcheck.new({
163+
name = "testing",
164+
shm_name = "test_shm",
165+
checks = {
166+
passive = {
167+
healthy = {
168+
successes = 2,
169+
},
170+
unhealthy = {
171+
tcp_failures = 2,
172+
http_failures = 2,
173+
}
174+
}
175+
}
176+
})
177+
ngx.sleep(0.1) -- wait for initial timers to run once
178+
checker:add_target("127.0.0.1", 2112, "rush", true)
179+
checker:add_target("127.0.0.2", 2112, "rush", true)
180+
checker:set_all_target_statuses_for_hostname("rush", 2112, false)
181+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false
182+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false
183+
checker:report_http_status("127.0.0.1", 2112, "rush", 200)
184+
checker:set_all_target_statuses_for_hostname("rush", 2112, false)
185+
checker:report_http_status("127.0.0.1", 2112, "rush", 200)
186+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false
187+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false
188+
checker:report_http_status("127.0.0.1", 2112, "rush", 200)
189+
ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true
190+
ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false
191+
}
192+
}
193+
--- request
194+
GET /t
195+
--- response_body
196+
false
197+
false
198+
false
199+
false
200+
true
201+
false

0 commit comments

Comments
 (0)