From 5abe3fa5090b68f703f767e9701b607dd052b269 Mon Sep 17 00:00:00 2001 From: yejingx Date: Thu, 13 Apr 2017 17:53:55 +0800 Subject: [PATCH] fix: do not get peer status if heartbeat not enabled --- lib/resty/checkups/base.lua | 11 +++++------ lib/resty/checkups/try.lua | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/resty/checkups/base.lua b/lib/resty/checkups/base.lua index f2e11db..ccb1b67 100644 --- a/lib/resty/checkups/base.lua +++ b/lib/resty/checkups/base.lua @@ -69,6 +69,7 @@ _M.cluster_status = cluster_status _M.is_tab = function(t) return type(t) == "table" end _M.is_str = function(t) return type(t) == "string" end _M.is_num = function(t) return type(t) == "number" end +_M.is_nul = function(t) return t == nil or t == ngx.null end local function _gen_key(skey, srv) @@ -218,8 +219,8 @@ end function _M.get_peer_status(skey, srv) local peer_key = PEER_STATUS_PREFIX .. _gen_key(skey, srv) - local peer_status = cjson.decode(state:get(peer_key)) - return peer_status + local peer_status = state:get(peer_key) + return not _M.is_nul(peer_status) and cjson.decode(peer_status) or nil end @@ -236,10 +237,8 @@ function _M.get_upstream_status(skey) ups_status[level] = {} if servers and type(servers) == "table" and #servers > 0 then for _, srv in ipairs(servers) do - local peer_key = _gen_key(skey, srv) - local peer_status = cjson.decode(state:get(PEER_STATUS_PREFIX .. - peer_key)) or {} - peer_status.server = peer_key + local peer_status = _M.get_peer_status(skey, srv) or {} + peer_status.server = _gen_key(skey, srv) peer_status["weight"] = srv.weight peer_status["max_fails"] = srv.max_fails peer_status["fail_timeout"] = srv.fail_timeout diff --git a/lib/resty/checkups/try.lua b/lib/resty/checkups/try.lua index 02bc63d..7566685 100644 --- a/lib/resty/checkups/try.lua +++ b/lib/resty/checkups/try.lua @@ -88,6 +88,11 @@ local function prepare_callbacks(skey, opts) return false end + if ups.enable == false or (ups.enable == nil + and base.upstream.default_heartbeat_enable == false) then + return base.get_srv_status(skey, srv) == base.STATUS_OK + end + local peer_status = base.get_peer_status(skey, srv) if (not peer_status or peer_status.status ~= base.STATUS_ERR) and base.get_srv_status(skey, srv) == base.STATUS_OK then