From 019e5db73f2d491abe7b3e1eca041ed2711e1456 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Fri, 18 Sep 2020 13:08:11 +0200 Subject: [PATCH] feat(*) add header support for active checks --- README.md | 1 + lib/resty/healthcheck.lua | 11 +++- t/20-req-headers.t | 114 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 t/20-req-headers.t diff --git a/README.md b/README.md index 105a2ea8..2582917d 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Versioning is strictly based on [Semantic Versioning](https://semver.org/) * fix: when not providing a hostname, use IP [#48](https://github.com/Kong/lua-resty-healthcheck/pull/48) * fix: makefile; make install * feature: added a status version field [#54](https://github.com/Kong/lua-resty-healthcheck/pull/54) +* feature: add headers for probe request [#54](https://github.com/Kong/lua-resty-healthcheck/pull/54) ### 1.3.0 (17-Jun-2020) diff --git a/lib/resty/healthcheck.lua b/lib/resty/healthcheck.lua index e3bdd1d7..58eeeb8d 100644 --- a/lib/resty/healthcheck.lua +++ b/lib/resty/healthcheck.lua @@ -850,8 +850,15 @@ function checker:run_single_check(ip, port, hostname, hostheader) end + local req_headers = self.checks.active.headers + local headers = table.concat(req_headers, "\r\n") + if #headers > 0 then + headers = headers .. "\r\n" + end + local path = self.checks.active.http_path - local request = ("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n"):format(path, hostheader or hostname) + local request = ("GET %s HTTP/1.0\r\n%sHost: %s\r\n\r\n"):format(path, headers, hostheader or hostname or ip) + self:log(DEBUG, "request head: ", request) local bytes bytes, err = sock:send(request) @@ -1229,6 +1236,7 @@ local defaults = { http_path = "/", https_sni = NO_DEFAULT, https_verify_certificate = true, + headers = {""}, healthy = { interval = 0, -- 0 = disabled by default http_statuses = { 200, 302 }, @@ -1301,6 +1309,7 @@ end -- * `checks.active.http_path`: path to use in `GET` HTTP request to run on active checks -- * `checks.active.https_sni`: SNI server name incase of HTTPS -- * `checks.active.https_verify_certificate`: boolean indicating whether to verify the HTTPS certificate +-- * `checks.active.hheaders`: an array of headers (no hash-table! must be pre-formatted) -- * `checks.active.healthy.interval`: interval between checks for healthy targets (in seconds) -- * `checks.active.healthy.http_statuses`: which HTTP statuses to consider a success -- * `checks.active.healthy.successes`: number of successes to consider a target healthy diff --git a/t/20-req-headers.t b/t/20-req-headers.t new file mode 100644 index 00000000..5e2ddb35 --- /dev/null +++ b/t/20-req-headers.t @@ -0,0 +1,114 @@ +use Test::Nginx::Socket::Lua 'no_plan'; +use Cwd qw(cwd); + +workers(1); + +my $pwd = cwd(); + +our $HttpConfig = qq{ + lua_package_path "$pwd/lib/?.lua;;"; + lua_shared_dict test_shm 8m; + lua_shared_dict my_worker_events 8m; +}; + +run_tests(); + +__DATA__ + +=== TEST 1: req_headers: {"User-Agent", "curl/7.29.0"} +--- http_config eval +qq{ + $::HttpConfig + + server { + listen 2112; + 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 checker = healthcheck.new({ + name = "testing", + shm_name = "test_shm", + checks = { + active = { + http_path = "/status", + healthy = { + interval = 0.1 + }, + req_headers = {"User-Agent: curl/7.29.0"} + } + } + }) + ngx.sleep(0.2) -- wait twice the interval + local ok, err = checker:add_target("127.0.0.1", 2112, nil, true) + ngx.say(ok) + ngx.sleep(0.2) -- wait twice the interval + } + } +--- request +GET /t +--- response_body +true +--- error_log +checking healthy targets: nothing to do +checking healthy targets: #1 +GET /status HTTP/1.0 +User-Agent: curl/7.29.0 +Host: 127.0.0.1 + + + +=== TEST 2: req_headers: {"User-Agent", "curl"} +--- http_config eval +qq{ + $::HttpConfig + + server { + listen 2112; + 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 checker = healthcheck.new({ + name = "testing", + shm_name = "test_shm", + checks = { + active = { + http_path = "/status", + healthy = { + interval = 0.1 + }, + req_headers = {"User-Agent: curl"} + } + } + }) + ngx.sleep(0.2) -- wait twice the interval + local ok, err = checker:add_target("127.0.0.1", 2112, nil, true) + ngx.say(ok) + ngx.sleep(0.2) -- wait twice the interval + } + } +--- request +GET /t +--- response_body +true +--- error_log +checking healthy targets: nothing to do +checking healthy targets: #1 +GET /status HTTP/1.0 +User-Agent: curl +Host: 127.0.0.1