Skip to content

Instantly share code, notes, and snippets.

@sitano
Last active March 20, 2017 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitano/73ae43e0733cc8158823005422b1f4af to your computer and use it in GitHub Desktop.
Save sitano/73ae43e0733cc8158823005422b1f4af to your computer and use it in GitHub Desktop.
openresty check upstreams status example
location = /status {
default_type text/plain;
content_by_lua '
local ok, upstream = pcall(require, "ngx.upstream")
if not ok then
error("ngx_upstream_lua module required")
end
local get_primary_peers = upstream.get_primary_peers
local get_backup_peers = upstream.get_backup_peers
local get_upstreams = upstream.get_upstreams
local function is_good(peers)
local npeers = #peers
for i = 1, npeers do
local peer = peers[i]
if not peer.down then
return true
end
end
return false
end
local us, err = get_upstreams()
if not us then
return "failed to get upstream names: " .. err
end
local n = #us
for i = 1, n do
local primary = get_primary_peers(us[i])
local backup = get_backup_peers(us[i])
if not is_good(primary) and not is_good(backup) then
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
ngx.say("unavailable")
return
end
end
ngx.status = ngx.HTTP_OK
ngx.say("ok")
';
@qingchundouzsu
Copy link

It seems that the line 18 statement "if not peer.down then" is forever true whatever the server is up or down because the down element is always nil .

I donnot know why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment