Skip to content

Commit 1458b5e

Browse files
committed
fix: improve least_conn balancer implementation
1 parent f00c56c commit 1458b5e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

apisix/balancer/least_conn.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ local _M = {}
3131
local CONN_COUNT_DICT_NAME = "balancer-least-conn"
3232
local conn_count_dict = ngx_shared[CONN_COUNT_DICT_NAME]
3333

34-
-- Initialize shared dictionary at module load time
35-
if not conn_count_dict then
34+
-- Check that the shared dictionary exists
35+
if ngx.shared and conn_count_dict == nil then
3636
core.log.error("shared dict '", CONN_COUNT_DICT_NAME, "' not found, ",
3737
"please add 'lua_shared_dict ", CONN_COUNT_DICT_NAME, " 10m;' ",
3838
"to your nginx configuration")
3939
error("shared dict '" .. CONN_COUNT_DICT_NAME .. "' not found")
4040
end
4141

42-
4342
local function least_score(a, b)
4443
return a.score < b.score
4544
end
@@ -132,6 +131,10 @@ end
132131

133132

134133
function _M.new(up_nodes, upstream)
134+
if not conn_count_dict then
135+
return nil, "dictionary not find"
136+
end
137+
135138
local servers_heap = binaryHeap.minUnique(least_score)
136139

137140
-- Clean up stale connection counts for removed servers

0 commit comments

Comments
 (0)