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
14 changes: 13 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ http {
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end

       -- Example of HealthCheck for Tcp
local ok, err = hc.spawn_checker{
shm = "healthcheck", -- defined by "lua_shared_dict"
           upstream = "foo.com", -- defined by "upstream"
type = "tcp",
interval = 2000, -- run the check cycle every 2 sec
timeout = 1000, -- 1 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
concurrency = 10, -- concurrency level for test requests
}

-- Just call hc.spawn_checker() for more times here if you have
       -- 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
13 changes: 10 additions & 3 deletions lib/resty/upstream/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ local function check_peer(ctx, id, peer, is_backup)
errlog("failed to connect to ", name, ": ", err)
end
return peer_fail(ctx, is_backup, id, peer)
else
if ctx.type == "tcp" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to move the if block a level out, so the close & return inside it is more explicitly. Like if not ok ... end; if ctx.type == ....

peer_ok(ctx, is_backup, id, peer)
sock:close()
return
end
end

local bytes, err = sock:send(req)
Expand Down Expand Up @@ -524,12 +530,12 @@ function _M.spawn_checker(opts)
return nil, "\"type\" option required"
end

if typ ~= "http" then
return nil, "only \"http\" type is supported right now"
if typ ~= "http" and typ ~= "tcp" then
return nil, "only \"http\" or \"tcp\" type is supported right now"
end

local http_req = opts.http_req
if not http_req then
if typ == "http" and not http_req then
return nil, "\"http_req\" option required"
end

Expand Down Expand Up @@ -602,6 +608,7 @@ function _M.spawn_checker(opts)
end

local ctx = {
type = typ,
upstream = u,
primary_peers = preprocess_peers(ppeers),
backup_peers = preprocess_peers(bpeers),
Expand Down