|
0 |
|
|
1 |
/*
|
|
2 |
* Copyright (C) Maxim Dounin
|
|
3 |
* Copyright (C) Nginx, Inc.
|
|
4 |
*/
|
|
5 |
|
|
6 |
|
|
7 |
#include <ngx_config.h>
|
|
8 |
#include <ngx_core.h>
|
|
9 |
#include <ngx_http.h>
|
|
10 |
|
|
11 |
|
|
12 |
typedef struct {
|
|
13 |
ngx_uint_t *conns;
|
|
14 |
} ngx_http_upstream_least_conn_conf_t;
|
|
15 |
|
|
16 |
|
|
17 |
typedef struct {
|
|
18 |
/* the round robin data must be first */
|
|
19 |
ngx_http_upstream_rr_peer_data_t rrp;
|
|
20 |
|
|
21 |
ngx_uint_t *conns;
|
|
22 |
|
|
23 |
ngx_event_get_peer_pt get_rr_peer;
|
|
24 |
ngx_event_free_peer_pt free_rr_peer;
|
|
25 |
} ngx_http_upstream_lc_peer_data_t;
|
|
26 |
|
|
27 |
|
|
28 |
static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
|
29 |
ngx_http_upstream_srv_conf_t *us);
|
|
30 |
static ngx_int_t ngx_http_upstream_get_least_conn_peer(
|
|
31 |
ngx_peer_connection_t *pc, void *data);
|
|
32 |
static void ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
|
|
33 |
void *data, ngx_uint_t state);
|
|
34 |
static void *ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf);
|
|
35 |
static char *ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
36 |
void *conf);
|
|
37 |
|
|
38 |
|
|
39 |
static ngx_command_t ngx_http_upstream_least_conn_commands[] = {
|
|
40 |
|
|
41 |
{ ngx_string("least_conn"),
|
|
42 |
NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
|
|
43 |
ngx_http_upstream_least_conn,
|
|
44 |
0,
|
|
45 |
0,
|
|
46 |
NULL },
|
|
47 |
|
|
48 |
ngx_null_command
|
|
49 |
};
|
|
50 |
|
|
51 |
|
|
52 |
static ngx_http_module_t ngx_http_upstream_least_conn_module_ctx = {
|
|
53 |
NULL, /* preconfiguration */
|
|
54 |
NULL, /* postconfiguration */
|
|
55 |
|
|
56 |
NULL, /* create main configuration */
|
|
57 |
NULL, /* init main configuration */
|
|
58 |
|
|
59 |
ngx_http_upstream_least_conn_create_conf, /* create server configuration */
|
|
60 |
NULL, /* merge server configuration */
|
|
61 |
|
|
62 |
NULL, /* create location configuration */
|
|
63 |
NULL /* merge location configuration */
|
|
64 |
};
|
|
65 |
|
|
66 |
|
|
67 |
ngx_module_t ngx_http_upstream_least_conn_module = {
|
|
68 |
NGX_MODULE_V1,
|
|
69 |
&ngx_http_upstream_least_conn_module_ctx, /* module context */
|
|
70 |
ngx_http_upstream_least_conn_commands, /* module directives */
|
|
71 |
NGX_HTTP_MODULE, /* module type */
|
|
72 |
NULL, /* init master */
|
|
73 |
NULL, /* init module */
|
|
74 |
NULL, /* init process */
|
|
75 |
NULL, /* init thread */
|
|
76 |
NULL, /* exit thread */
|
|
77 |
NULL, /* exit process */
|
|
78 |
NULL, /* exit master */
|
|
79 |
NGX_MODULE_V1_PADDING
|
|
80 |
};
|
|
81 |
|
|
82 |
|
|
83 |
ngx_int_t
|
|
84 |
ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
|
|
85 |
ngx_http_upstream_srv_conf_t *us)
|
|
86 |
{
|
|
87 |
ngx_uint_t n;
|
|
88 |
ngx_http_upstream_rr_peers_t *peers;
|
|
89 |
ngx_http_upstream_least_conn_conf_t *lcf;
|
|
90 |
|
|
91 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
|
92 |
"init least conn");
|
|
93 |
|
|
94 |
if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
|
|
95 |
return NGX_ERROR;
|
|
96 |
}
|
|
97 |
|
|
98 |
peers = us->peer.data;
|
|
99 |
|
|
100 |
n = peers->number;
|
|
101 |
|
|
102 |
if (peers->next) {
|
|
103 |
n += peers->next->number;
|
|
104 |
}
|
|
105 |
|
|
106 |
lcf = ngx_http_conf_upstream_srv_conf(us,
|
|
107 |
ngx_http_upstream_least_conn_module);
|
|
108 |
|
|
109 |
lcf->conns = ngx_pcalloc(cf->pool, sizeof(ngx_uint_t) * n);
|
|
110 |
if (lcf->conns == NULL) {
|
|
111 |
return NGX_ERROR;
|
|
112 |
}
|
|
113 |
|
|
114 |
us->peer.init = ngx_http_upstream_init_least_conn_peer;
|
|
115 |
|
|
116 |
return NGX_OK;
|
|
117 |
}
|
|
118 |
|
|
119 |
|
|
120 |
static ngx_int_t
|
|
121 |
ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
|
122 |
ngx_http_upstream_srv_conf_t *us)
|
|
123 |
{
|
|
124 |
ngx_http_upstream_lc_peer_data_t *lcp;
|
|
125 |
ngx_http_upstream_least_conn_conf_t *lcf;
|
|
126 |
|
|
127 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
128 |
"init least conn peer");
|
|
129 |
|
|
130 |
lcf = ngx_http_conf_upstream_srv_conf(us,
|
|
131 |
ngx_http_upstream_least_conn_module);
|
|
132 |
|
|
133 |
lcp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_lc_peer_data_t));
|
|
134 |
if (lcp == NULL) {
|
|
135 |
return NGX_ERROR;
|
|
136 |
}
|
|
137 |
|
|
138 |
lcp->conns = lcf->conns;
|
|
139 |
|
|
140 |
r->upstream->peer.data = &lcp->rrp;
|
|
141 |
|
|
142 |
if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
|
|
143 |
return NGX_ERROR;
|
|
144 |
}
|
|
145 |
|
|
146 |
r->upstream->peer.get = ngx_http_upstream_get_least_conn_peer;
|
|
147 |
r->upstream->peer.free = ngx_http_upstream_free_least_conn_peer;
|
|
148 |
|
|
149 |
lcp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
|
|
150 |
lcp->free_rr_peer = ngx_http_upstream_free_round_robin_peer;
|
|
151 |
|
|
152 |
return NGX_OK;
|
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
static ngx_int_t
|
|
157 |
ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|
158 |
{
|
|
159 |
ngx_http_upstream_lc_peer_data_t *lcp = data;
|
|
160 |
|
|
161 |
time_t now;
|
|
162 |
uintptr_t m;
|
|
163 |
ngx_int_t rc, total;
|
|
164 |
ngx_uint_t i, n, p, many;
|
|
165 |
ngx_http_upstream_rr_peer_t *peer, *best;
|
|
166 |
ngx_http_upstream_rr_peers_t *peers;
|
|
167 |
|
|
168 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
169 |
"get least conn peer, try: %ui", pc->tries);
|
|
170 |
|
|
171 |
if (lcp->rrp.peers->single) {
|
|
172 |
return lcp->get_rr_peer(pc, &lcp->rrp);
|
|
173 |
}
|
|
174 |
|
|
175 |
pc->cached = 0;
|
|
176 |
pc->connection = NULL;
|
|
177 |
|
|
178 |
now = ngx_time();
|
|
179 |
|
|
180 |
peers = lcp->rrp.peers;
|
|
181 |
|
|
182 |
best = NULL;
|
|
183 |
total = 0;
|
|
184 |
|
|
185 |
#if (NGX_SUPPRESS_WARN)
|
|
186 |
many = 0;
|
|
187 |
p = 0;
|
|
188 |
#endif
|
|
189 |
|
|
190 |
for (i = 0; i < peers->number; i++) {
|
|
191 |
|
|
192 |
n = i / (8 * sizeof(uintptr_t));
|
|
193 |
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
|
194 |
|
|
195 |
if (lcp->rrp.tried[n] & m) {
|
|
196 |
continue;
|
|
197 |
}
|
|
198 |
|
|
199 |
peer = &peers->peer[i];
|
|
200 |
|
|
201 |
if (peer->down) {
|
|
202 |
continue;
|
|
203 |
}
|
|
204 |
|
|
205 |
if (peer->max_fails
|
|
206 |
&& peer->fails >= peer->max_fails
|
|
207 |
&& now - peer->checked <= peer->fail_timeout)
|
|
208 |
{
|
|
209 |
continue;
|
|
210 |
}
|
|
211 |
|
|
212 |
/*
|
|
213 |
* select peer with least number of connections; if there are
|
|
214 |
* multiple peers with the same number of connections, select
|
|
215 |
* based on round-robin
|
|
216 |
*/
|
|
217 |
|
|
218 |
if (best == NULL
|
|
219 |
|| lcp->conns[i] * best->weight < lcp->conns[p] * peer->weight)
|
|
220 |
{
|
|
221 |
best = peer;
|
|
222 |
many = 0;
|
|
223 |
p = i;
|
|
224 |
|
|
225 |
} else if (lcp->conns[i] * best->weight
|
|
226 |
== lcp->conns[p] * peer->weight)
|
|
227 |
{
|
|
228 |
many = 1;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
if (best == NULL) {
|
|
233 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
234 |
"get least conn peer, no peer found");
|
|
235 |
|
|
236 |
goto failed;
|
|
237 |
}
|
|
238 |
|
|
239 |
if (many) {
|
|
240 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
241 |
"get least conn peer, many");
|
|
242 |
|
|
243 |
for (i = p; i < peers->number; i++) {
|
|
244 |
|
|
245 |
n = i / (8 * sizeof(uintptr_t));
|
|
246 |
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
|
247 |
|
|
248 |
if (lcp->rrp.tried[n] & m) {
|
|
249 |
continue;
|
|
250 |
}
|
|
251 |
|
|
252 |
peer = &peers->peer[i];
|
|
253 |
|
|
254 |
if (peer->down) {
|
|
255 |
continue;
|
|
256 |
}
|
|
257 |
|
|
258 |
if (lcp->conns[i] * best->weight != lcp->conns[p] * peer->weight) {
|
|
259 |
continue;
|
|
260 |
}
|
|
261 |
|
|
262 |
if (peer->max_fails
|
|
263 |
&& peer->fails >= peer->max_fails
|
|
264 |
&& now - peer->checked <= peer->fail_timeout)
|
|
265 |
{
|
|
266 |
continue;
|
|
267 |
}
|
|
268 |
|
|
269 |
peer->current_weight += peer->effective_weight;
|
|
270 |
total += peer->effective_weight;
|
|
271 |
|
|
272 |
if (peer->effective_weight < peer->weight) {
|
|
273 |
peer->effective_weight++;
|
|
274 |
}
|
|
275 |
|
|
276 |
if (peer->current_weight > best->current_weight) {
|
|
277 |
best = peer;
|
|
278 |
p = i;
|
|
279 |
}
|
|
280 |
}
|
|
281 |
}
|
|
282 |
|
|
283 |
best->current_weight -= total;
|
|
284 |
best->checked = now;
|
|
285 |
|
|
286 |
pc->sockaddr = best->sockaddr;
|
|
287 |
pc->socklen = best->socklen;
|
|
288 |
pc->name = &best->name;
|
|
289 |
|
|
290 |
lcp->rrp.current = p;
|
|
291 |
|
|
292 |
n = p / (8 * sizeof(uintptr_t));
|
|
293 |
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
|
294 |
|
|
295 |
lcp->rrp.tried[n] |= m;
|
|
296 |
lcp->conns[p]++;
|
|
297 |
|
|
298 |
if (pc->tries == 1 && peers->next) {
|
|
299 |
pc->tries += peers->next->number;
|
|
300 |
}
|
|
301 |
|
|
302 |
return NGX_OK;
|
|
303 |
|
|
304 |
failed:
|
|
305 |
|
|
306 |
if (peers->next) {
|
|
307 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
308 |
"get least conn peer, backup servers");
|
|
309 |
|
|
310 |
lcp->conns += peers->number;
|
|
311 |
|
|
312 |
lcp->rrp.peers = peers->next;
|
|
313 |
pc->tries = lcp->rrp.peers->number;
|
|
314 |
|
|
315 |
n = lcp->rrp.peers->number / (8 * sizeof(uintptr_t)) + 1;
|
|
316 |
for (i = 0; i < n; i++) {
|
|
317 |
lcp->rrp.tried[i] = 0;
|
|
318 |
}
|
|
319 |
|
|
320 |
rc = ngx_http_upstream_get_least_conn_peer(pc, lcp);
|
|
321 |
|
|
322 |
if (rc != NGX_BUSY) {
|
|
323 |
return rc;
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
/* all peers failed, mark them as live for quick recovery */
|
|
328 |
|
|
329 |
for (i = 0; i < peers->number; i++) {
|
|
330 |
peers->peer[i].fails = 0;
|
|
331 |
}
|
|
332 |
|
|
333 |
pc->name = peers->name;
|
|
334 |
|
|
335 |
return NGX_BUSY;
|
|
336 |
}
|
|
337 |
|
|
338 |
|
|
339 |
static void
|
|
340 |
ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
|
|
341 |
void *data, ngx_uint_t state)
|
|
342 |
{
|
|
343 |
ngx_http_upstream_lc_peer_data_t *lcp = data;
|
|
344 |
|
|
345 |
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
346 |
"free least conn peer %ui %ui", pc->tries, state);
|
|
347 |
|
|
348 |
if (lcp->rrp.peers->single) {
|
|
349 |
lcp->free_rr_peer(pc, &lcp->rrp, state);
|
|
350 |
return;
|
|
351 |
}
|
|
352 |
|
|
353 |
if (state == 0 && pc->tries == 0) {
|
|
354 |
return;
|
|
355 |
}
|
|
356 |
|
|
357 |
lcp->conns[lcp->rrp.current]--;
|
|
358 |
|
|
359 |
lcp->free_rr_peer(pc, &lcp->rrp, state);
|
|
360 |
}
|
|
361 |
|
|
362 |
|
|
363 |
static void *
|
|
364 |
ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf)
|
|
365 |
{
|
|
366 |
ngx_http_upstream_least_conn_conf_t *conf;
|
|
367 |
|
|
368 |
conf = ngx_pcalloc(cf->pool,
|
|
369 |
sizeof(ngx_http_upstream_least_conn_conf_t));
|
|
370 |
if (conf == NULL) {
|
|
371 |
return NULL;
|
|
372 |
}
|
|
373 |
|
|
374 |
/*
|
|
375 |
* set by ngx_pcalloc():
|
|
376 |
*
|
|
377 |
* conf->conns = NULL;
|
|
378 |
*/
|
|
379 |
|
|
380 |
return conf;
|
|
381 |
}
|
|
382 |
|
|
383 |
|
|
384 |
static char *
|
|
385 |
ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
386 |
{
|
|
387 |
ngx_http_upstream_srv_conf_t *uscf;
|
|
388 |
|
|
389 |
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
|
|
390 |
|
|
391 |
uscf->peer.init_upstream = ngx_http_upstream_init_least_conn;
|
|
392 |
|
|
393 |
uscf->flags = NGX_HTTP_UPSTREAM_CREATE
|
|
394 |
|NGX_HTTP_UPSTREAM_WEIGHT
|
|
395 |
|NGX_HTTP_UPSTREAM_MAX_FAILS
|
|
396 |
|NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|
|
397 |
|NGX_HTTP_UPSTREAM_DOWN
|
|
398 |
|NGX_HTTP_UPSTREAM_BACKUP;
|
|
399 |
|
|
400 |
return NGX_CONF_OK;
|
|
401 |
}
|