Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(balancer) avoid crash when failing to obtain list of upstreams #4301

Merged
merged 1 commit into from
Feb 12, 2019
Merged
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
29 changes: 16 additions & 13 deletions kong/runloop/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -466,35 +466,38 @@ end

local get_all_upstreams
do
------------------------------------------------------------------------------
-- Implements a simple dictionary with all upstream-ids indexed
-- by their name.
-- @return The upstreams dictionary, a map with upstream names as string keys
-- and upstream entity tables as values, or nil+error
local function load_upstreams_dict_into_memory()
local upstreams_dict = {}
for up in singletons.db.upstreams:each(1000) do
-- build a dictionary, indexed by the upstream name
for up, err in singletons.db.upstreams:each(1000) do
if err then
log(CRIT, "could not obtain list of upstreams: ", err)
return nil
end

upstreams_dict[up.name] = up.id
end
return upstreams_dict
end
_load_upstreams_dict_into_memory = load_upstreams_dict_into_memory


local opts = { neg_ttl = 10 }


------------------------------------------------------------------------------
-- Finds and returns an upstream entity. This function covers
-- caching, invalidation, db access, et al.
-- @param upstream_name string.
-- @return upstream table, or `false` if not found, or nil+error
-- Implements a simple dictionary with all upstream-ids indexed
-- by their name.
-- @return The upstreams dictionary (a map with upstream names as string keys
-- and upstream entity tables as values), or nil+error
get_all_upstreams = function()
local upstreams_dict, err = singletons.cache:get("balancer:upstreams", nil,
local upstreams_dict, err = singletons.cache:get("balancer:upstreams", opts,
load_upstreams_dict_into_memory)
if err then
return nil, err
end

return upstreams_dict
return upstreams_dict or {}
end
end

Expand Down Expand Up @@ -718,7 +721,7 @@ local function init()

local upstreams, err = get_all_upstreams()
if not upstreams then
log(CRIT, "failed loading initial list of upstreams: " .. err)
log(CRIT, "failed loading initial list of upstreams: ", err)
return
end

Expand Down