@@ -24,26 +24,16 @@ local ngx = ngx
24
24
local ngx_shared = ngx .shared
25
25
local tostring = tostring
26
26
27
-
28
27
local _M = {}
29
28
30
29
-- Shared dictionary to store connection counts across balancer recreations
31
30
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
41
32
42
33
local function least_score (a , b )
43
34
return a .score < b .score
44
35
end
45
36
46
-
47
37
-- Get the connection count key for a specific upstream and server
48
38
local function get_conn_count_key (upstream , server )
49
39
local upstream_id = upstream .id
@@ -129,10 +119,14 @@ local function cleanup_stale_conn_counts(upstream, current_servers)
129
119
end
130
120
end
131
121
132
-
133
122
function _M .new (up_nodes , upstream )
134
123
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"
136
130
end
137
131
138
132
local servers_heap = binaryHeap .minUnique (least_score )
@@ -162,7 +156,7 @@ function _M.new(up_nodes, upstream)
162
156
163
157
return {
164
158
upstream = upstream ,
165
- get = function (ctx )
159
+ get = function (ctx )
166
160
local server , info , err
167
161
if ctx .balancer_tried_servers then
168
162
local tried_server_list = {}
@@ -201,7 +195,7 @@ function _M.new(up_nodes, upstream)
201
195
incr_server_conn_count (upstream , server , 1 )
202
196
return server
203
197
end ,
204
- after_balance = function (ctx , before_retry )
198
+ after_balance = function (ctx , before_retry )
205
199
local server = ctx .balancer_server
206
200
local info = servers_heap :valueByPayload (server )
207
201
if not info then
@@ -233,7 +227,7 @@ function _M.new(up_nodes, upstream)
233
227
234
228
ctx .balancer_tried_servers [server ] = true
235
229
end ,
236
- before_retry_next_priority = function (ctx )
230
+ before_retry_next_priority = function (ctx )
237
231
if ctx .balancer_tried_servers then
238
232
core .tablepool .release (" balancer_tried_servers" , ctx .balancer_tried_servers )
239
233
ctx .balancer_tried_servers = nil
@@ -242,6 +236,5 @@ function _M.new(up_nodes, upstream)
242
236
}
243
237
end
244
238
245
-
246
239
return _M
247
240
0 commit comments