Skip to content

Commit

Permalink
feature: support to user define request headers.
Browse files Browse the repository at this point in the history
feature: added `status_ver` field, when the status changes, `status_ver` automatically +1.
  • Loading branch information
membphis committed Apr 8, 2020
1 parent 68f0cc6 commit d05fa0c
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,15 @@ function checker:run_single_check(ip, port, hostname, hostheader)
end
end

local req_headers = self.checks.active.req_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)
Expand Down Expand Up @@ -1064,11 +1071,17 @@ function checker:event_handler(event_name, ip, port, hostname)
self:log(DEBUG, "event: target added '", hostname or "", "(", ip, ":", port, ")'")
end
do
local from = target_found.internal_health
local to = event_name
self:log(DEBUG, "event: target status '", hostname or "", "(", ip, ":", port,
")' from '", from == "healthy" or from == "mostly_healthy",
"' to '", to == "healthy" or to == "mostly_healthy", "'")
local from_status = target_found.internal_health
local to_status = event_name
local from = from_status == "healthy" or from_status == "mostly_healthy"
local to = to_status == "healthy" or to_status == "mostly_healthy"

if from ~= to then
self.status_ver = self.status_ver + 1
end

self:log(DEBUG, "event: target status '", hostname or "", "(", ip, ":",
port, ")' from '", from, "' to '", to, "', ver: ", self.status_ver)
end
target_found.internal_health = event_name

Expand Down Expand Up @@ -1202,6 +1215,7 @@ local defaults = {
name = NO_DEFAULT,
shm_name = NO_DEFAULT,
type = NO_DEFAULT,
status_ver = 0,
checks = {
active = {
type = "http",
Expand All @@ -1222,6 +1236,7 @@ local defaults = {
timeouts = 3,
http_failures = 5,
},
req_headers = {""},
},
passive = {
type = "http",
Expand Down Expand Up @@ -1387,7 +1402,7 @@ function _M.new(opts)
-- fill-in the hash part for easy lookup
self.targets[target.ip] = self.targets[target.ip] or {}
self.targets[target.ip][target.port] = self.targets[target.ip][target.port] or {}
self.targets[target.ip][target.port][target.hostname] = target
self.targets[target.ip][target.port][target.hostname or target.ip] = target
end

return true
Expand Down
114 changes: 114 additions & 0 deletions t/req-headers.t
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions t/status-ver.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);

workers(2);
master_on();

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;

init_worker_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
ngx.timer.at(0, function()
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
})
local ok, err = checker:add_target("127.0.0.1", 11111)
if not ok then
error(err)
end
end)
}
};

run_tests();

__DATA__

=== TEST 1: add_target() adds an unhealthy target
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
ngx.say(true)
ngx.sleep(0.3) -- wait twice the interval
}
}
--- request
GET /t
--- response_body
true
--- error_log
checking unhealthy targets: nothing to do
checking unhealthy targets: #1
from 'true' to 'false', ver: 2
from 'true' to 'false', ver: 1

0 comments on commit d05fa0c

Please sign in to comment.