Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ http {

# the size depends on the number of servers in upstream {}:
lua_shared_dict healthcheck 1m;
lua_shared_dict healthcheck_config 1m;

lua_socket_log_errors off;

Expand All @@ -65,6 +66,7 @@ http {
rise = 2, -- # of successive successes before turning a peer up
valid_statuses = {200, 302}, -- a list valid HTTP status code
concurrency = 10, -- concurrency level for test requests
shared_config = true, -- whether shared config between the worker
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
Expand Down Expand Up @@ -93,6 +95,40 @@ http {
ngx.print(hc.status_page())
}
}
# by this location can modify the conf passing to `spawn_checker` above
location = /health{
content_by_lua_block{
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.check_and_set_config{
shm = "healthcheck", -- defined by "lua_shared_dict"
upstream = "default_upstream", -- defined by "upstream"
type = "http",

http_req = "GET /status HTTP/1.0\r\nHost: localhost\r\n\r\n",
-- raw HTTP request for checking
interval = 7000, -- run the check cycle every 7 sec
timeout = 8000, -- 8 sec is the timeout for network operations
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
concurrency = 10, -- concurrency level for test requests
shared_config=true,
}

if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end

ngx.say(ngx.ERR,'-------------------------------',cjson.encode(ok),cjson.encode(context.healthcheck_config));


-- Just call hc.spawn_checker() for more times here if you have
-- more upstream groups to monitor. One call for one upstream group.
-- They can all share the same shm zone without conflicts but they
-- need a bigger shm zone for obvious reasons.
}
}
}
}
```
Expand Down
25 changes: 19 additions & 6 deletions lib/resty/upstream/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local ceil = math.ceil
local spawn = ngx.thread.spawn
local wait = ngx.thread.wait
local pcall = pcall
local shared_config = require("resty.upstream.shared_config")

local _M = {
_VERSION = '0.03'
Expand Down Expand Up @@ -491,6 +492,11 @@ check = function (premature, ctx)
return
end

if ctx.shared_config and shared_config:refresh() then
ctx = _M.check_and_set_config(shared_config:get())
shared_config:mark_worker_update_finish()
end

local ok, err = pcall(do_check, ctx)
if not ok then
errlog("failed to run healthcheck cycle: ", err)
Expand Down Expand Up @@ -524,7 +530,7 @@ local function preprocess_peers(peers)
return peers
end

function _M.spawn_checker(opts)
function _M.check_and_set_config( opts )
local typ = opts.type
if not typ then
return nil, "\"type\" option required"
Expand Down Expand Up @@ -606,8 +612,7 @@ function _M.spawn_checker(opts)
if not bpeers then
return nil, "failed to get backup peers: " .. err
end

local ctx = {
local ctx = {
upstream = u,
primary_peers = preprocess_peers(ppeers),
backup_peers = preprocess_peers(bpeers),
Expand All @@ -620,15 +625,23 @@ function _M.spawn_checker(opts)
statuses = statuses,
version = 0,
concurrency = concur,
shared_config = opts.shared_config or false and true
}

if ctx.shared_config then
shared_config:set(opts)
end

return ctx
end

function _M.spawn_checker(opts)
local ctx = _M.check_and_set_config(opts)
local ok, err = new_timer(0, check, ctx)
if not ok then
return nil, "failed to create timer: " .. err
end

update_upstream_checker_status(u, true)

update_upstream_checker_status( ctx.upstream, true)
return true
end

Expand Down
40 changes: 40 additions & 0 deletions lib/resty/upstream/shared_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local _M = {}
local shared_config = ngx.shared.healthcheck_config
local no_update_flag = 'config_no_update'
local config_flag = 'config_json_data'
local cjson = require("cjson")
cjson.encode_sparse_array(true)

function _M:refresh()
local no_update,err = shared_config:get(no_update_flag .. ngx.worker.id())
return not no_update
end

function _M:mark_worker_update_finish()
shared_config:set(no_update_flag .. ngx.worker.id(),true)
end

function _M:mark_worker_shared_conf_updated()
local worker_num = ngx.worker.count() - 1
for i =0 , worker_num do
shared_config:set(no_update_flag .. i , false)
end
end

function _M:get()
local v,err = shared_config:get(config_flag)

if not v then
ngx.log(ngx.ERR,err)
return ngx.null
end

return cjson.decode(v)
end

function _M:set(conf)
shared_config:set(config_flag,cjson.encode(conf))
self:mark_worker_shared_conf_updated()
end

return _M
1 change: 1 addition & 0 deletions t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ my $pwd = cwd();
our $HttpConfig = <<_EOC_;
lua_socket_log_errors off;
lua_package_path "$pwd/../lua-resty-lock/?.lua;$pwd/lib/?.lua;$pwd/t/lib/?.lua;;";
lua_shared_dict healthcheck_config 1m;
_EOC_

#no_diff();
Expand Down