Skip to content

Commit 8211383

Browse files
fix(balancer) remove the correct upstream event from queue
here the `i` variable is an index to a copy of the event queue, but then it's used to remove the event from the original queue. As soon as one item is removed, the indexes don't match and the wrong event is removed. this patch works on the first event and then removes the first item on each iteration. If an event results in an error, the loop is aborted and it's not removed from the queue, just as previously. If the error is persistent, no other event is processed. Consider moving failed events to the tail of the queue.
1 parent 0809eae commit 8211383

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

kong/runloop/balancer.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ local healthcheck_subscribers = {}
9292
-- functions forward-declarations
9393
local create_balancers
9494
local set_upstream_events_queue
95-
local get_upstream_events_queue
9695

9796
local function set_balancer(upstream_id, balancer)
9897
local prev = balancers[upstream_id]
@@ -853,11 +852,6 @@ do
853852
}
854853
end
855854

856-
857-
get_upstream_events_queue = function()
858-
return utils.deep_copy(upstream_events_queue)
859-
end
860-
861855
end
862856

863857

@@ -866,18 +860,16 @@ local function update_balancer_state(premature)
866860
return
867861
end
868862

869-
local events_queue = get_upstream_events_queue()
870-
871-
for i, v in ipairs(events_queue) do
872-
-- handle the oldest (first) event from the queue
863+
while upstream_events_queue[1] do
864+
local v = upstream_events_queue[1]
873865
local _, err = do_upstream_event(v.operation, v.upstream_data, v.workspaces)
874866
if err then
875867
log(CRIT, "failed handling upstream event: ", err)
876868
return
877869
end
878870

879871
-- if no err, remove the upstream event from the queue
880-
table_remove(upstream_events_queue, i)
872+
table_remove(upstream_events_queue, 1)
881873
end
882874

883875
local frequency = kong.configuration.worker_state_update_frequency or 1

0 commit comments

Comments
 (0)