Mercurial > hg > nginx
annotate src/stream/ngx_stream_upstream_least_conn_module.c @ 6626:b3682580c1bd
Avoid left-shifting integers into the sign bit, which is undefined.
Found with UndefinedBehaviorSanitizer.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Thu, 07 Jul 2016 21:02:28 +0300 |
parents | 2f41d383c9c7 |
children | 8ed51b02f655 |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Maxim Dounin | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 static ngx_int_t ngx_stream_upstream_init_least_conn_peer( | |
14 ngx_stream_session_t *s, ngx_stream_upstream_srv_conf_t *us); | |
15 static ngx_int_t ngx_stream_upstream_get_least_conn_peer( | |
16 ngx_peer_connection_t *pc, void *data); | |
17 static char *ngx_stream_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, | |
18 void *conf); | |
19 | |
20 | |
21 static ngx_command_t ngx_stream_upstream_least_conn_commands[] = { | |
22 | |
23 { ngx_string("least_conn"), | |
24 NGX_STREAM_UPS_CONF|NGX_CONF_NOARGS, | |
25 ngx_stream_upstream_least_conn, | |
26 0, | |
27 0, | |
28 NULL }, | |
29 | |
30 ngx_null_command | |
31 }; | |
32 | |
33 | |
34 static ngx_stream_module_t ngx_stream_upstream_least_conn_module_ctx = { | |
6606
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
35 NULL, /* preconfiguration */ |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
36 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
37 |
6115 | 38 NULL, /* create main configuration */ |
39 NULL, /* init main configuration */ | |
40 | |
41 NULL, /* create server configuration */ | |
42 NULL, /* merge server configuration */ | |
43 }; | |
44 | |
45 | |
46 ngx_module_t ngx_stream_upstream_least_conn_module = { | |
47 NGX_MODULE_V1, | |
48 &ngx_stream_upstream_least_conn_module_ctx, /* module context */ | |
49 ngx_stream_upstream_least_conn_commands, /* module directives */ | |
50 NGX_STREAM_MODULE, /* module type */ | |
51 NULL, /* init master */ | |
52 NULL, /* init module */ | |
53 NULL, /* init process */ | |
54 NULL, /* init thread */ | |
55 NULL, /* exit thread */ | |
56 NULL, /* exit process */ | |
57 NULL, /* exit master */ | |
58 NGX_MODULE_V1_PADDING | |
59 }; | |
60 | |
61 | |
62 static ngx_int_t | |
63 ngx_stream_upstream_init_least_conn(ngx_conf_t *cf, | |
64 ngx_stream_upstream_srv_conf_t *us) | |
65 { | |
66 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, cf->log, 0, | |
67 "init least conn"); | |
68 | |
69 if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) { | |
70 return NGX_ERROR; | |
71 } | |
72 | |
73 us->peer.init = ngx_stream_upstream_init_least_conn_peer; | |
74 | |
75 return NGX_OK; | |
76 } | |
77 | |
78 | |
79 static ngx_int_t | |
80 ngx_stream_upstream_init_least_conn_peer(ngx_stream_session_t *s, | |
81 ngx_stream_upstream_srv_conf_t *us) | |
82 { | |
83 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
84 "init least conn peer"); | |
85 | |
86 if (ngx_stream_upstream_init_round_robin_peer(s, us) != NGX_OK) { | |
87 return NGX_ERROR; | |
88 } | |
89 | |
90 s->upstream->peer.get = ngx_stream_upstream_get_least_conn_peer; | |
91 | |
92 return NGX_OK; | |
93 } | |
94 | |
95 | |
96 static ngx_int_t | |
97 ngx_stream_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data) | |
98 { | |
99 ngx_stream_upstream_rr_peer_data_t *rrp = data; | |
100 | |
101 time_t now; | |
102 uintptr_t m; | |
103 ngx_int_t rc, total; | |
104 ngx_uint_t i, n, p, many; | |
105 ngx_stream_upstream_rr_peer_t *peer, *best; | |
106 ngx_stream_upstream_rr_peers_t *peers; | |
107 | |
108 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
109 "get least conn peer, try: %ui", pc->tries); | |
110 | |
111 if (rrp->peers->single) { | |
112 return ngx_stream_upstream_get_round_robin_peer(pc, rrp); | |
113 } | |
114 | |
115 pc->connection = NULL; | |
116 | |
117 now = ngx_time(); | |
118 | |
119 peers = rrp->peers; | |
120 | |
121 ngx_stream_upstream_rr_peers_wlock(peers); | |
122 | |
123 best = NULL; | |
124 total = 0; | |
125 | |
126 #if (NGX_SUPPRESS_WARN) | |
127 many = 0; | |
128 p = 0; | |
129 #endif | |
130 | |
131 for (peer = peers->peer, i = 0; | |
132 peer; | |
133 peer = peer->next, i++) | |
134 { | |
135 | |
136 n = i / (8 * sizeof(uintptr_t)); | |
137 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); | |
138 | |
139 if (rrp->tried[n] & m) { | |
140 continue; | |
141 } | |
142 | |
143 if (peer->down) { | |
144 continue; | |
145 } | |
146 | |
147 if (peer->max_fails | |
148 && peer->fails >= peer->max_fails | |
149 && now - peer->checked <= peer->fail_timeout) | |
150 { | |
151 continue; | |
152 } | |
153 | |
154 /* | |
155 * select peer with least number of connections; if there are | |
156 * multiple peers with the same number of connections, select | |
157 * based on round-robin | |
158 */ | |
159 | |
160 if (best == NULL | |
161 || peer->conns * best->weight < best->conns * peer->weight) | |
162 { | |
163 best = peer; | |
164 many = 0; | |
165 p = i; | |
166 | |
167 } else if (peer->conns * best->weight == best->conns * peer->weight) { | |
168 many = 1; | |
169 } | |
170 } | |
171 | |
172 if (best == NULL) { | |
173 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
174 "get least conn peer, no peer found"); | |
175 | |
176 goto failed; | |
177 } | |
178 | |
179 if (many) { | |
180 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
181 "get least conn peer, many"); | |
182 | |
183 for (peer = best, i = p; | |
184 peer; | |
185 peer = peer->next, i++) | |
186 { | |
187 n = i / (8 * sizeof(uintptr_t)); | |
188 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); | |
189 | |
190 if (rrp->tried[n] & m) { | |
191 continue; | |
192 } | |
193 | |
194 if (peer->down) { | |
195 continue; | |
196 } | |
197 | |
198 if (peer->conns * best->weight != best->conns * peer->weight) { | |
199 continue; | |
200 } | |
201 | |
202 if (peer->max_fails | |
203 && peer->fails >= peer->max_fails | |
204 && now - peer->checked <= peer->fail_timeout) | |
205 { | |
206 continue; | |
207 } | |
208 | |
209 peer->current_weight += peer->effective_weight; | |
210 total += peer->effective_weight; | |
211 | |
212 if (peer->effective_weight < peer->weight) { | |
213 peer->effective_weight++; | |
214 } | |
215 | |
216 if (peer->current_weight > best->current_weight) { | |
217 best = peer; | |
218 p = i; | |
219 } | |
220 } | |
221 } | |
222 | |
223 best->current_weight -= total; | |
224 | |
225 if (now - best->checked > best->fail_timeout) { | |
226 best->checked = now; | |
227 } | |
228 | |
229 pc->sockaddr = best->sockaddr; | |
230 pc->socklen = best->socklen; | |
231 pc->name = &best->name; | |
232 | |
233 best->conns++; | |
234 | |
235 rrp->current = best; | |
236 | |
237 n = p / (8 * sizeof(uintptr_t)); | |
238 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); | |
239 | |
240 rrp->tried[n] |= m; | |
241 | |
242 ngx_stream_upstream_rr_peers_unlock(peers); | |
243 | |
244 return NGX_OK; | |
245 | |
246 failed: | |
247 | |
248 if (peers->next) { | |
249 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
250 "get least conn peer, backup servers"); | |
251 | |
252 rrp->peers = peers->next; | |
253 | |
254 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
255 / (8 * sizeof(uintptr_t)); | |
256 | |
257 for (i = 0; i < n; i++) { | |
6474 | 258 rrp->tried[i] = 0; |
6115 | 259 } |
260 | |
261 ngx_stream_upstream_rr_peers_unlock(peers); | |
262 | |
263 rc = ngx_stream_upstream_get_least_conn_peer(pc, rrp); | |
264 | |
265 if (rc != NGX_BUSY) { | |
266 return rc; | |
267 } | |
268 | |
269 ngx_stream_upstream_rr_peers_wlock(peers); | |
270 } | |
271 | |
272 /* all peers failed, mark them as live for quick recovery */ | |
273 | |
274 for (peer = peers->peer; peer; peer = peer->next) { | |
275 peer->fails = 0; | |
276 } | |
277 | |
278 ngx_stream_upstream_rr_peers_unlock(peers); | |
279 | |
280 pc->name = peers->name; | |
281 | |
282 return NGX_BUSY; | |
283 } | |
284 | |
285 | |
286 static char * | |
287 ngx_stream_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
288 { | |
289 ngx_stream_upstream_srv_conf_t *uscf; | |
290 | |
291 uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); | |
292 | |
293 if (uscf->peer.init_upstream) { | |
294 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
295 "load balancing method redefined"); | |
296 } | |
297 | |
298 uscf->peer.init_upstream = ngx_stream_upstream_init_least_conn; | |
299 | |
300 uscf->flags = NGX_STREAM_UPSTREAM_CREATE | |
301 |NGX_STREAM_UPSTREAM_WEIGHT | |
302 |NGX_STREAM_UPSTREAM_MAX_FAILS | |
303 |NGX_STREAM_UPSTREAM_FAIL_TIMEOUT | |
304 |NGX_STREAM_UPSTREAM_DOWN | |
305 |NGX_STREAM_UPSTREAM_BACKUP; | |
306 | |
307 return NGX_CONF_OK; | |
308 } |