Skip to content

Commit 14d4440

Browse files
authored
feat: add check health running mode function (#149)
1 parent ec1fb01 commit 14d4440

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

health_check.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Implement a passive health check mechanism, that when the connection/read/write
99
* [init](#init)
1010
* [report_failure](#report_failure)
1111
* [get_target_status](#get_target_status)
12+
* [get_check_mode](#get_check_mode)
1213

1314
### init
1415

@@ -28,6 +29,15 @@ Reports a health failure which will count against the number of occurrences requ
2829

2930
Get the current status of the target.
3031

32+
### get_check_mode
33+
34+
`syntax: mode = health_check.get_check_mode()`
35+
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.
37+
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.
39+
40+
3141
## Config
3242

3343
| name | Type | Requirement | Default | Description |

lib/resty/etcd/health_check.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ local type = type
44
local now = os.time
55
local conf
66

7+
local HEALTH_CHECK_MODE_ROUND_ROBIN = "round-robin"
8+
local HEALTH_CHECK_MODE_SHARED_DICT = "shared-dict"
9+
710
local _M = {}
11+
_M.ROUND_ROBIN_MODE = HEALTH_CHECK_MODE_ROUND_ROBIN
12+
_M.SHARED_DICT_MODE = HEALTH_CHECK_MODE_SHARED_DICT
813

914
local round_robin_unhealthy_target_hosts
1015

@@ -102,6 +107,18 @@ end
102107
_M.report_failure = report_failure
103108

104109

110+
local function get_check_mode()
111+
-- round-robin: nginx worker memory round-robin based health check
112+
-- 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
117+
end
118+
end
119+
_M.get_check_mode = get_check_mode
120+
121+
105122
function _M.init(opts)
106123
opts = opts or {}
107124
if not conf or opts.shm_name ~= conf.shm_name then

t/v3/health_check.t

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,46 @@ http://127.0.0.1:42379: connection refused
767767
http://127.0.0.1:22379: OK
768768
--- error_log eval
769769
qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
770+
771+
772+
773+
=== TEST 19: test health check running mode
774+
--- http_config eval: $::HttpConfig
775+
--- config
776+
location /t {
777+
content_by_lua_block {
778+
local etcd, err = require "resty.etcd" .new({
779+
protocol = "v3",
780+
http_host = {
781+
"http://127.0.0.1:12379",
782+
"http://127.0.0.1:22379",
783+
"http://127.0.0.1:32379",
784+
},
785+
})
786+
787+
local health_check = require("resty.etcd.health_check")
788+
local mode = health_check.get_check_mode()
789+
if mode == health_check.ROUND_ROBIN_MODE then
790+
ngx.say("passed")
791+
end
792+
793+
health_check.init({
794+
shm_name = "etcd_cluster_health_check",
795+
})
796+
797+
mode = health_check.get_check_mode()
798+
if mode == health_check.SHARED_DICT_MODE then
799+
ngx.say("passed")
800+
end
801+
}
802+
}
803+
--- request
804+
GET /t
805+
--- response_body
806+
passed
807+
passed
808+
--- grep_error_log eval
809+
qr/healthy check use \S+ \w+/
810+
--- grep_error_log_out
811+
healthy check use round robin
812+
healthy check use ngx.shared dict

0 commit comments

Comments
 (0)