Skip to content

Commit 7386b9c

Browse files
authored
feat: allow disabling health check (#163)
Signed-off-by: spacewander <[email protected]>
1 parent 70b648f commit 7386b9c

File tree

3 files changed

+114
-6
lines changed

3 files changed

+114
-6
lines changed

health_check.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ Get the current status of the target.
3333

3434
`syntax: mode = health_check.get_check_mode()`
3535

36-
Get the current health check running mode. When `mode` is `round-robin`, it means running in [Round robin](#round-robin-based-health-check) mode, and `shared-dict` means running in [Policy](#policy-based-health-check) mode operation.
36+
Get the current health check running mode. When `mode` is `round-robin`, it means running in [Round robin](#round-robin-based-health-check) mode, and `shared-dict` means running in [Policy](#policy-based-health-check) mode operation, `disabled` means the health check is disabled.
3737

38-
When using this method to get the health check running mode, you can use `health_check.ROUND_ROBIN_MODE` and `health_check.SHARED_DICT_MODE` to judge and compare.
38+
When using this method to get the health check running mode, you can use `health_check.ROUND_ROBIN_MODE` and `health_check.SHARED_DICT_MODE`, `health_check.DISABLED_MODE` to judge and compare.
3939

40+
### disable
41+
42+
`syntax: mode = health_check.disable()`
43+
44+
Disable health check.
4045

4146
## Config
4247

lib/resty/etcd/health_check.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ local conf
66

77
local HEALTH_CHECK_MODE_ROUND_ROBIN = "round-robin"
88
local HEALTH_CHECK_MODE_SHARED_DICT = "shared-dict"
9+
local HEALTH_CHECK_MODE_DISABLED = "disabled"
910

1011
local _M = {}
1112
_M.ROUND_ROBIN_MODE = HEALTH_CHECK_MODE_ROUND_ROBIN
1213
_M.SHARED_DICT_MODE = HEALTH_CHECK_MODE_SHARED_DICT
14+
_M.DISABLED_MODE = HEALTH_CHECK_MODE_DISABLED
1315

1416
local round_robin_unhealthy_target_hosts
1517

@@ -23,6 +25,10 @@ local function get_target_status(etcd_host)
2325
return nil, "etcd health check uninitialized"
2426
end
2527

28+
if conf.disabled then
29+
return true
30+
end
31+
2632
if type(etcd_host) ~= "string" then
2733
return false, "etcd host invalid"
2834
end
@@ -75,6 +81,10 @@ local function report_failure(etcd_host)
7581
return nil, "etcd health check uninitialized"
7682
end
7783

84+
if conf.disabled then
85+
return
86+
end
87+
7888
if type(etcd_host) ~= "string" then
7989
return nil, "etcd host invalid"
8090
end
@@ -110,15 +120,29 @@ _M.report_failure = report_failure
110120
local function get_check_mode()
111121
-- round-robin: nginx worker memory round-robin based health check
112122
-- shared-dict: nginx shared memory policy based health check
113-
if conf and conf.shm_name then
114-
return HEALTH_CHECK_MODE_SHARED_DICT
115-
else
116-
return HEALTH_CHECK_MODE_ROUND_ROBIN
123+
if conf then
124+
if conf.disabled then
125+
return HEALTH_CHECK_MODE_DISABLED
126+
elseif conf.shm_name then
127+
return HEALTH_CHECK_MODE_SHARED_DICT
128+
end
117129
end
130+
131+
return HEALTH_CHECK_MODE_ROUND_ROBIN
118132
end
119133
_M.get_check_mode = get_check_mode
120134

121135

136+
function _M.disable()
137+
if not conf then
138+
conf = {}
139+
end
140+
141+
conf.disabled = true
142+
_M.conf = conf
143+
end
144+
145+
122146
function _M.init(opts)
123147
opts = opts or {}
124148
if not conf or opts.shm_name ~= conf.shm_name then

t/v3/health_check.t

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,3 +810,82 @@ qr/healthy check use \S+ \w+/
810810
--- grep_error_log_out
811811
healthy check use round robin
812812
healthy check use ngx.shared dict
813+
814+
815+
816+
=== TEST 20: disable health check
817+
--- http_config eval: $::HttpConfig
818+
--- config
819+
location /t {
820+
content_by_lua_block {
821+
local health_check = require("resty.etcd.health_check")
822+
health_check.disable()
823+
local etcd, err = require "resty.etcd" .new({
824+
protocol = "v3",
825+
http_host = {
826+
"http://127.0.0.1:42379",
827+
"http://127.0.0.1:22379",
828+
"http://127.0.0.1:32379",
829+
},
830+
})
831+
832+
local res
833+
res, err = etcd:set("/test/etcd/unhealthy", "hello")
834+
ngx.say(err)
835+
res, err = etcd:set("/test/etcd/healthy", "hello")
836+
if err == nil then
837+
ngx.say("http://127.0.0.1:22379: OK")
838+
else
839+
ngx.say(err)
840+
end
841+
}
842+
}
843+
--- request
844+
GET /t
845+
--- response_body
846+
http://127.0.0.1:42379: connection refused
847+
http://127.0.0.1:42379: connection refused
848+
--- no_error_log eval
849+
qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
850+
851+
852+
853+
=== TEST 21: health check disabled mode
854+
--- http_config eval: $::HttpConfig
855+
--- config
856+
location /t {
857+
content_by_lua_block {
858+
local etcd, err = require "resty.etcd" .new({
859+
protocol = "v3",
860+
http_host = {
861+
"http://127.0.0.1:12379",
862+
"http://127.0.0.1:22379",
863+
"http://127.0.0.1:32379",
864+
},
865+
})
866+
867+
local health_check = require("resty.etcd.health_check")
868+
local mode = health_check.get_check_mode()
869+
870+
health_check.init({
871+
shm_name = "etcd_cluster_health_check",
872+
})
873+
874+
mode = health_check.get_check_mode()
875+
if mode == health_check.SHARED_DICT_MODE then
876+
ngx.say("passed")
877+
end
878+
879+
health_check.disable()
880+
881+
mode = health_check.get_check_mode()
882+
if mode == health_check.DISABLED_MODE then
883+
ngx.say("passed")
884+
end
885+
}
886+
}
887+
--- request
888+
GET /t
889+
--- response_body
890+
passed
891+
passed

0 commit comments

Comments
 (0)