Skip to content

Commit

Permalink
feat: add meta support for healthcheck instance method add_target
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 committed Oct 9, 2023
1 parent 94882e5 commit add2df2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,9 @@ end
-- default is `true`.
-- @param hostheader (optional) a value to use for the Host header on
-- active healthchecks.
-- @param tbl_meta (optional) a lua table with custom info of business stuff
-- @return `true` on success, or `nil + error` on failure.
function checker:add_target(ip, port, hostname, is_healthy, hostheader)
function checker:add_target(ip, port, hostname, is_healthy, hostheader, tbl_meta)
ip = tostring(assert(ip, "no ip address provided"))
port = assert(tonumber(port), "no port number provided")
hostname = hostname or ip
Expand Down Expand Up @@ -531,6 +532,7 @@ function checker:add_target(ip, port, hostname, is_healthy, hostheader)
port = port,
hostname = hostname,
hostheader = hostheader,
meta = tbl_meta,
}
end
target_list = serialize(target_list)
Expand Down Expand Up @@ -1272,6 +1274,7 @@ local function checker_callback(self, health_mode)
port = target.port,
hostname = target.hostname,
hostheader = target.hostheader,
meta = target.meta,
debug_health = internal_health,
}
end
Expand Down
62 changes: 62 additions & 0 deletions t/with_resty-events/98-get_target_list.t
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,65 @@ qq{
--- request
GET /t
--- timeout: 5



=== TEST 2: healthcheck - add_target with meta
--- http_config eval
qq{
$::HttpConfig

server {
listen 2116;
location = /status {
return 200;
}
}
}
--- config
location = /t {
content_by_lua_block {
local healthcheck = require("resty.healthcheck")
local name = "testing"
local shm_name = "test_shm"
local checker = healthcheck.new({
name = name,
shm_name = shm_name,
events_module = "resty.events",
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1, -- we don't want active checks
successes = 1,
},
unhealthy = {
interval = 0.1, -- we don't want active checks
tcp_failures = 3,
http_failures = 3,
}
}
}
})
checker:add_target("127.0.0.1", 2116, nil, false, nil, { raw = "host_1" })
checker:add_target("127.0.0.2", 2116, nil, false, nil, { raw = "host_2" })
ngx.sleep(2)
local nodes = healthcheck.get_target_list(name, shm_name)
assert(#nodes == 2, "invalid number of nodes")
for _, node in ipairs(nodes) do
assert(node.ip == "127.0.0.1" or node.ip == "127.0.0.2", "invalid ip")
assert(node.port == 2116, "invalid port")
assert(node.status == "healthy", "invalid status")
assert(node.counter.success == 1, "invalid success counter")
assert(node.counter.tcp_failure == 0, "invalid tcp failure counter")
assert(node.counter.http_failure == 0, "invalid http failure counter")
assert(node.counter.timeout_failure == 0, "invalid timeout failure counter")
assert(node.meta.raw == "host_1" or node.meta.raw == "host_2", "invalid node meta")
end
}
}
--- request
GET /t
--- no_error_log
[error]
61 changes: 61 additions & 0 deletions t/with_worker-events/98-get_target_list.t
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,64 @@ qq{
--- request
GET /t
--- timeout: 5



=== TEST 2: healthcheck - add_target with meta
--- http_config eval
qq{
$::HttpConfig

server {
listen 2116;
location = /status {
return 200;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local name = "testing"
local shm_name = "test_shm"
local checker = healthcheck.new({
name = name,
shm_name = shm_name,
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1, -- we don't want active checks
successes = 1,
},
unhealthy = {
interval = 0.1, -- we don't want active checks
tcp_failures = 3,
http_failures = 3,
}
}
}
})
checker:add_target("127.0.0.1", 2116, nil, false, nil, { raw = "host_1" })
checker:add_target("127.0.0.2", 2116, nil, false, nil, { raw = "host_2" })
ngx.sleep(3)
local nodes = healthcheck.get_target_list(name, shm_name)
assert(#nodes == 2, "invalid number of nodes")
for _, node in ipairs(nodes) do
assert(node.ip == "127.0.0.1" or node.ip == "127.0.0.2", "invalid ip")
assert(node.port == 2116, "invalid port")
assert(node.status == "healthy", "invalid status")
assert(node.counter.success == 1, "invalid success counter")
assert(node.counter.tcp_failure == 0, "invalid tcp failure counter")
assert(node.counter.http_failure == 0, "invalid http failure counter")
assert(node.counter.timeout_failure == 0, "invalid timeout failure counter")
assert(node.meta.raw == "host_1" or node.meta.raw == "host_2", "invalid node meta")
end
}
}
--- request
GET /t

0 comments on commit add2df2

Please sign in to comment.