Mercurial > hg > nginx
annotate src/http/modules/ngx_http_upstream_least_conn_module.c @ 6470:62d950bc0f15
Updated OpenSSL used for win32 builds.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 29 Mar 2016 09:54:11 +0300 |
parents | 3264b7828f72 |
children | 2cd019520210 |
rev | line source |
---|---|
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
1 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
2 /* |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
3 * Copyright (C) Maxim Dounin |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
4 * Copyright (C) Nginx, Inc. |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
5 */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
6 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
7 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
9 #include <ngx_core.h> |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
10 #include <ngx_http.h> |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
11 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
12 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
13 static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
14 ngx_http_upstream_srv_conf_t *us); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
15 static ngx_int_t ngx_http_upstream_get_least_conn_peer( |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
16 ngx_peer_connection_t *pc, void *data); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
17 static char *ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
18 void *conf); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
19 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
20 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
21 static ngx_command_t ngx_http_upstream_least_conn_commands[] = { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
22 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
23 { ngx_string("least_conn"), |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
24 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
25 ngx_http_upstream_least_conn, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
26 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
27 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
28 NULL }, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
29 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
30 ngx_null_command |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
31 }; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
32 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
33 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
34 static ngx_http_module_t ngx_http_upstream_least_conn_module_ctx = { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
35 NULL, /* preconfiguration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
36 NULL, /* postconfiguration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
37 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
38 NULL, /* create main configuration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
39 NULL, /* init main configuration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
40 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
41 NULL, /* create server configuration */ |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
42 NULL, /* merge server configuration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
43 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
44 NULL, /* create location configuration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
45 NULL /* merge location configuration */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
46 }; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
47 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
48 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
49 ngx_module_t ngx_http_upstream_least_conn_module = { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
50 NGX_MODULE_V1, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
51 &ngx_http_upstream_least_conn_module_ctx, /* module context */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
52 ngx_http_upstream_least_conn_commands, /* module directives */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
53 NGX_HTTP_MODULE, /* module type */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
54 NULL, /* init master */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
55 NULL, /* init module */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
56 NULL, /* init process */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
57 NULL, /* init thread */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
58 NULL, /* exit thread */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
59 NULL, /* exit process */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
60 NULL, /* exit master */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
61 NGX_MODULE_V1_PADDING |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
62 }; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
63 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
64 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4656
diff
changeset
|
65 static ngx_int_t |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
66 ngx_http_upstream_init_least_conn(ngx_conf_t *cf, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
67 ngx_http_upstream_srv_conf_t *us) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
68 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
69 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
70 "init least conn"); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
71 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
72 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
73 return NGX_ERROR; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
74 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
75 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
76 us->peer.init = ngx_http_upstream_init_least_conn_peer; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
77 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
78 return NGX_OK; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
79 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
80 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
81 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
82 static ngx_int_t |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
83 ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
84 ngx_http_upstream_srv_conf_t *us) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
85 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
86 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
87 "init least conn peer"); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
88 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
89 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
90 return NGX_ERROR; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
91 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
92 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
93 r->upstream->peer.get = ngx_http_upstream_get_least_conn_peer; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
94 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
95 return NGX_OK; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
96 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
97 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
98 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
99 static ngx_int_t |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
100 ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
101 { |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
102 ngx_http_upstream_rr_peer_data_t *rrp = data; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
103 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
104 time_t now; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
105 uintptr_t m; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
106 ngx_int_t rc, total; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
107 ngx_uint_t i, n, p, many; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
108 ngx_http_upstream_rr_peer_t *peer, *best; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
109 ngx_http_upstream_rr_peers_t *peers; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
110 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
111 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
112 "get least conn peer, try: %ui", pc->tries); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
113 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
114 if (rrp->peers->single) { |
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
115 return ngx_http_upstream_get_round_robin_peer(pc, rrp); |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
116 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
117 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
118 pc->cached = 0; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
119 pc->connection = NULL; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
120 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
121 now = ngx_time(); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
122 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
123 peers = rrp->peers; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
124 |
6102 | 125 ngx_http_upstream_rr_peers_wlock(peers); |
126 | |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
127 best = NULL; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
128 total = 0; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
129 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
130 #if (NGX_SUPPRESS_WARN) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
131 many = 0; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
132 p = 0; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
133 #endif |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
134 |
6100
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
135 for (peer = peers->peer, i = 0; |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
136 peer; |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
137 peer = peer->next, i++) |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
138 { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
139 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
140 n = i / (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
141 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
142 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
143 if (rrp->tried[n] & m) { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
144 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
145 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
146 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
147 if (peer->down) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
148 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
149 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
150 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
151 if (peer->max_fails |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
152 && peer->fails >= peer->max_fails |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
153 && now - peer->checked <= peer->fail_timeout) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
154 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
155 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
156 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
157 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
158 /* |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
159 * select peer with least number of connections; if there are |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
160 * multiple peers with the same number of connections, select |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
161 * based on round-robin |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
162 */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
163 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
164 if (best == NULL |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
165 || peer->conns * best->weight < best->conns * peer->weight) |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
166 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
167 best = peer; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
168 many = 0; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
169 p = i; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
170 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
171 } else if (peer->conns * best->weight == best->conns * peer->weight) { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
172 many = 1; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
173 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
174 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
175 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
176 if (best == NULL) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
177 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
178 "get least conn peer, no peer found"); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
179 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
180 goto failed; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
181 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
182 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
183 if (many) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
184 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
185 "get least conn peer, many"); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
186 |
6100
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
187 for (peer = best, i = p; |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
188 peer; |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
189 peer = peer->next, i++) |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
190 { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
191 n = i / (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
192 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
193 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
194 if (rrp->tried[n] & m) { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
195 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
196 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
197 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
198 if (peer->down) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
199 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
200 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
201 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
202 if (peer->conns * best->weight != best->conns * peer->weight) { |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
203 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
204 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
205 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
206 if (peer->max_fails |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
207 && peer->fails >= peer->max_fails |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
208 && now - peer->checked <= peer->fail_timeout) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
209 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
210 continue; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
211 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
212 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
213 peer->current_weight += peer->effective_weight; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
214 total += peer->effective_weight; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
215 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
216 if (peer->effective_weight < peer->weight) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
217 peer->effective_weight++; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
218 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
219 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
220 if (peer->current_weight > best->current_weight) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
221 best = peer; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
222 p = i; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
223 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
224 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
225 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
226 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
227 best->current_weight -= total; |
5220
1d68b502088c
Upstream: fixed fail_timeout and max_fails > 1.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5173
diff
changeset
|
228 |
1d68b502088c
Upstream: fixed fail_timeout and max_fails > 1.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5173
diff
changeset
|
229 if (now - best->checked > best->fail_timeout) { |
1d68b502088c
Upstream: fixed fail_timeout and max_fails > 1.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5173
diff
changeset
|
230 best->checked = now; |
1d68b502088c
Upstream: fixed fail_timeout and max_fails > 1.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5173
diff
changeset
|
231 } |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
232 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
233 pc->sockaddr = best->sockaddr; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
234 pc->socklen = best->socklen; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
235 pc->name = &best->name; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
236 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
237 best->conns++; |
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
238 |
6100
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
239 rrp->current = best; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
240 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
241 n = p / (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
242 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
243 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
244 rrp->tried[n] |= m; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
245 |
6102 | 246 ngx_http_upstream_rr_peers_unlock(peers); |
247 | |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
248 return NGX_OK; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
249 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
250 failed: |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
251 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
252 if (peers->next) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
253 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
254 "get least conn peer, backup servers"); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
255 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
256 rrp->peers = peers->next; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
257 |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
258 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) |
4991
a384c60d55f3
Upstream: fixed state resetting when switching to backup servers.
Valentin Bartenev <vbart@nginx.com>
parents:
4759
diff
changeset
|
259 / (8 * sizeof(uintptr_t)); |
a384c60d55f3
Upstream: fixed state resetting when switching to backup servers.
Valentin Bartenev <vbart@nginx.com>
parents:
4759
diff
changeset
|
260 |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
261 for (i = 0; i < n; i++) { |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
262 rrp->tried[i] = 0; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
263 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
264 |
6102 | 265 ngx_http_upstream_rr_peers_unlock(peers); |
266 | |
6099
6ff0ebd6fbf4
Upstream: track the number of active connections to upstreams.
Ruslan Ermilov <ru@nginx.com>
parents:
5831
diff
changeset
|
267 rc = ngx_http_upstream_get_least_conn_peer(pc, rrp); |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
268 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
269 if (rc != NGX_BUSY) { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
270 return rc; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
271 } |
6102 | 272 |
273 ngx_http_upstream_rr_peers_wlock(peers); | |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
274 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
275 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
276 /* all peers failed, mark them as live for quick recovery */ |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
277 |
6100
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
278 for (peer = peers->peer; peer; peer = peer->next) { |
c44459611d91
Upstream: store peers as a linked list.
Ruslan Ermilov <ru@nginx.com>
parents:
6099
diff
changeset
|
279 peer->fails = 0; |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
280 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
281 |
6102 | 282 ngx_http_upstream_rr_peers_unlock(peers); |
283 | |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
284 pc->name = peers->name; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
285 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
286 return NGX_BUSY; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
287 } |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
288 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
289 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
290 static char * |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
291 ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
292 { |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
293 ngx_http_upstream_srv_conf_t *uscf; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
294 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
295 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
296 |
5173
5373be93c0be
Upstream: warn if multiple non-stackable balancers are installed.
Ruslan Ermilov <ru@nginx.com>
parents:
5133
diff
changeset
|
297 if (uscf->peer.init_upstream) { |
5373be93c0be
Upstream: warn if multiple non-stackable balancers are installed.
Ruslan Ermilov <ru@nginx.com>
parents:
5133
diff
changeset
|
298 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
5373be93c0be
Upstream: warn if multiple non-stackable balancers are installed.
Ruslan Ermilov <ru@nginx.com>
parents:
5133
diff
changeset
|
299 "load balancing method redefined"); |
5373be93c0be
Upstream: warn if multiple non-stackable balancers are installed.
Ruslan Ermilov <ru@nginx.com>
parents:
5133
diff
changeset
|
300 } |
5373be93c0be
Upstream: warn if multiple non-stackable balancers are installed.
Ruslan Ermilov <ru@nginx.com>
parents:
5133
diff
changeset
|
301 |
4654
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
302 uscf->peer.init_upstream = ngx_http_upstream_init_least_conn; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
303 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
304 uscf->flags = NGX_HTTP_UPSTREAM_CREATE |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
305 |NGX_HTTP_UPSTREAM_WEIGHT |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
306 |NGX_HTTP_UPSTREAM_MAX_FAILS |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
307 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
308 |NGX_HTTP_UPSTREAM_DOWN |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
309 |NGX_HTTP_UPSTREAM_BACKUP; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
310 |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
311 return NGX_CONF_OK; |
a2ca3a6ee680
Upstream: least_conn balancer module.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
312 } |