Skip to content

Commit 1105564

Browse files
committed
fix: improve least_conn balancer implementation
1 parent ac0fc95 commit 1105564

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

apisix/balancer/least_conn.lua

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,16 @@ local ngx = ngx
2424
local ngx_shared = ngx.shared
2525
local tostring = tostring
2626

27-
2827
local _M = {}
2928

3029
-- Shared dictionary to store connection counts across balancer recreations
3130
local CONN_COUNT_DICT_NAME = "balancer-least-conn"
32-
local conn_count_dict = ngx_shared[CONN_COUNT_DICT_NAME]
33-
34-
-- Check that the shared dictionary exists
35-
if ngx.shared and conn_count_dict == nil then
36-
core.log.error("shared dict '", CONN_COUNT_DICT_NAME, "' not found, ",
37-
"please add 'lua_shared_dict ", CONN_COUNT_DICT_NAME, " 10m;' ",
38-
"to your nginx configuration")
39-
error("shared dict '" .. CONN_COUNT_DICT_NAME .. "' not found")
40-
end
31+
local conn_count_dict
4132

4233
local function least_score(a, b)
4334
return a.score < b.score
4435
end
4536

46-
4737
-- Get the connection count key for a specific upstream and server
4838
local function get_conn_count_key(upstream, server)
4939
local upstream_id = upstream.id
@@ -129,10 +119,14 @@ local function cleanup_stale_conn_counts(upstream, current_servers)
129119
end
130120
end
131121

132-
133122
function _M.new(up_nodes, upstream)
134123
if not conn_count_dict then
135-
return nil, "dictionary not find"
124+
conn_count_dict = ngx_shared[CONN_COUNT_DICT_NAME]
125+
end
126+
127+
if not conn_count_dict then
128+
core.log.error("shared dict '", CONN_COUNT_DICT_NAME, "' not found")
129+
return nil, "shared dict not found"
136130
end
137131

138132
local servers_heap = binaryHeap.minUnique(least_score)
@@ -162,7 +156,7 @@ function _M.new(up_nodes, upstream)
162156

163157
return {
164158
upstream = upstream,
165-
get = function (ctx)
159+
get = function(ctx)
166160
local server, info, err
167161
if ctx.balancer_tried_servers then
168162
local tried_server_list = {}
@@ -201,7 +195,7 @@ function _M.new(up_nodes, upstream)
201195
incr_server_conn_count(upstream, server, 1)
202196
return server
203197
end,
204-
after_balance = function (ctx, before_retry)
198+
after_balance = function(ctx, before_retry)
205199
local server = ctx.balancer_server
206200
local info = servers_heap:valueByPayload(server)
207201
if not info then
@@ -233,7 +227,7 @@ function _M.new(up_nodes, upstream)
233227

234228
ctx.balancer_tried_servers[server] = true
235229
end,
236-
before_retry_next_priority = function (ctx)
230+
before_retry_next_priority = function(ctx)
237231
if ctx.balancer_tried_servers then
238232
core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
239233
ctx.balancer_tried_servers = nil
@@ -242,6 +236,5 @@ function _M.new(up_nodes, upstream)
242236
}
243237
end
244238

245-
246239
return _M
247240

0 commit comments

Comments
 (0)