From 7b96bdd483680c284771ef365dfa210f9493684a Mon Sep 17 00:00:00 2001 From: Cloud User Date: Sun, 20 Nov 2016 13:49:28 +0000 Subject: [PATCH 1/5] Add the possibility to match a string in the beginning of the body --- README.markdown | 2 ++ lib/resty/upstream/healthcheck.lua | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.markdown b/README.markdown index 961a636..5fe1178 100644 --- a/README.markdown +++ b/README.markdown @@ -2,6 +2,7 @@ Name ==== lua-resty-upstream-healthcheck - Health-checker for Nginx upstream servers +Add the possibility to match a specific string in the beginning of the body Table of Contents ================= @@ -64,6 +65,7 @@ http { fall = 3, -- # of successive failures before turning a peer down rise = 2, -- # of successive successes before turning a peer up valid_statuses = {200, 302}, -- a list valid HTTP status code + match_string = "OkiDoki", -- a string to match in the beginning of the body concurrency = 10, -- concurrency level for test requests } if not ok then diff --git a/lib/resty/upstream/healthcheck.lua b/lib/resty/upstream/healthcheck.lua index 2118282..1d93e0a 100644 --- a/lib/resty/upstream/healthcheck.lua +++ b/lib/resty/upstream/healthcheck.lua @@ -6,6 +6,7 @@ local WARN = ngx.WARN local DEBUG = ngx.DEBUG local str_find = string.find local sub = string.sub +local re_match = ngx.re.match local re_find = ngx.re.find local new_timer = ngx.timer.at local shared = ngx.shared @@ -214,6 +215,7 @@ local function check_peer(ctx, id, peer, is_backup) local name = peer.name local statuses = ctx.statuses local req = ctx.http_req + local match_string = ctx.match_string local sock, err = stream_sock() if not sock then @@ -273,6 +275,18 @@ local function check_peer(ctx, id, peer, is_backup) end end + if match_string then + local data, err = sock:receive('*a') + local m, err = re_match(data, match_string, "jo") + if not m then + peer_error(ctx, is_backup, id, peer, + "match_string not found from ", name, ": ", + data) + sock:close() + return + end + end + peer_ok(ctx, is_backup, id, peer) sock:close() end @@ -567,6 +581,11 @@ function _M.spawn_checker(opts) -- debug("interval: ", interval) + local match_string = opts.match_string + if not match_string then + match_string = "OKI" + end + local concur = opts.concurrency if not concur then concur = 1 @@ -617,6 +636,7 @@ function _M.spawn_checker(opts) dict = dict, fall = fall, rise = rise, + match_string = match_string, statuses = statuses, version = 0, concurrency = concur, From c525cc7b995f8c40943356de58058b2d1c93239b Mon Sep 17 00:00:00 2001 From: Cloud User Date: Sun, 20 Nov 2016 13:51:09 +0000 Subject: [PATCH 2/5] Add an example --- README.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/README.markdown b/README.markdown index 5fe1178..78ff44d 100644 --- a/README.markdown +++ b/README.markdown @@ -2,6 +2,7 @@ Name ==== lua-resty-upstream-healthcheck - Health-checker for Nginx upstream servers + Add the possibility to match a specific string in the beginning of the body Table of Contents From a2cc3927c16cbdcdf22759c071eb2f006dd31c26 Mon Sep 17 00:00:00 2001 From: Cloud User Date: Sun, 20 Nov 2016 17:42:30 +0000 Subject: [PATCH 3/5] Read only first few bytes from the upstream --- lib/resty/upstream/healthcheck.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/resty/upstream/healthcheck.lua b/lib/resty/upstream/healthcheck.lua index 1d93e0a..aeeaea7 100644 --- a/lib/resty/upstream/healthcheck.lua +++ b/lib/resty/upstream/healthcheck.lua @@ -276,12 +276,12 @@ local function check_peer(ctx, id, peer, is_backup) end if match_string then - local data, err = sock:receive('*a') + local bytes, err, partial = sock:receive(2048) -- read the first 2k of data + local data = partial or bytes local m, err = re_match(data, match_string, "jo") if not m then peer_error(ctx, is_backup, id, peer, - "match_string not found from ", name, ": ", - data) + "match_string not found from ", name, ": ", match_string) sock:close() return end From b6b0f56e629cf9105d0799415cfef012982687be Mon Sep 17 00:00:00 2001 From: Cloud User Date: Sun, 20 Nov 2016 17:59:05 +0000 Subject: [PATCH 4/5] 1k seems ok --- lib/resty/upstream/healthcheck.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/upstream/healthcheck.lua b/lib/resty/upstream/healthcheck.lua index aeeaea7..962521d 100644 --- a/lib/resty/upstream/healthcheck.lua +++ b/lib/resty/upstream/healthcheck.lua @@ -276,7 +276,7 @@ local function check_peer(ctx, id, peer, is_backup) end if match_string then - local bytes, err, partial = sock:receive(2048) -- read the first 2k of data + local bytes, err, partial = sock:receive(1024) -- read the first 1k of data local data = partial or bytes local m, err = re_match(data, match_string, "jo") if not m then From 4d1962cffd7e5d3e7cc35411d7fe35debee43133 Mon Sep 17 00:00:00 2001 From: Cloud User Date: Sun, 20 Nov 2016 18:03:40 +0000 Subject: [PATCH 5/5] Not mandatory --- lib/resty/upstream/healthcheck.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/upstream/healthcheck.lua b/lib/resty/upstream/healthcheck.lua index 962521d..8e76a7b 100644 --- a/lib/resty/upstream/healthcheck.lua +++ b/lib/resty/upstream/healthcheck.lua @@ -583,7 +583,7 @@ function _M.spawn_checker(opts) local match_string = opts.match_string if not match_string then - match_string = "OKI" + match_string = nil end local concur = opts.concurrency