From add2df2bb570ea098af30528881a37a530a2a8a8 Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 Date: Mon, 9 Oct 2023 16:28:29 +0800 Subject: [PATCH] feat: add meta support for healthcheck instance method add_target --- lib/resty/healthcheck.lua | 5 +- t/with_resty-events/98-get_target_list.t | 62 +++++++++++++++++++++++ t/with_worker-events/98-get_target_list.t | 61 ++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/lib/resty/healthcheck.lua b/lib/resty/healthcheck.lua index 61b6b6c..64a0979 100644 --- a/lib/resty/healthcheck.lua +++ b/lib/resty/healthcheck.lua @@ -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 @@ -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) @@ -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 diff --git a/t/with_resty-events/98-get_target_list.t b/t/with_resty-events/98-get_target_list.t index e8ff92a..321a3c6 100644 --- a/t/with_resty-events/98-get_target_list.t +++ b/t/with_resty-events/98-get_target_list.t @@ -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] diff --git a/t/with_worker-events/98-get_target_list.t b/t/with_worker-events/98-get_target_list.t index 6734047..b2d1f2a 100644 --- a/t/with_worker-events/98-get_target_list.t +++ b/t/with_worker-events/98-get_target_list.t @@ -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