Mercurial > hg > nginx
comparison src/http/modules/ngx_http_upstream_keepalive_module.c @ 4127:b2e21a39307c
Upstream keepalive module.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 15 Sep 2011 19:28:18 +0000 |
parents | |
children | d620f497c50f |
comparison
equal
deleted
inserted
replaced
4126:bba9a5ccc6cd | 4127:b2e21a39307c |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Maxim Dounin | |
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 max_cached; | |
14 ngx_uint_t single; /* unsigned:1 */ | |
15 | |
16 ngx_queue_t cache; | |
17 ngx_queue_t free; | |
18 | |
19 ngx_http_upstream_init_pt original_init_upstream; | |
20 ngx_http_upstream_init_peer_pt original_init_peer; | |
21 | |
22 } ngx_http_upstream_keepalive_srv_conf_t; | |
23 | |
24 | |
25 typedef struct { | |
26 ngx_http_upstream_keepalive_srv_conf_t *conf; | |
27 | |
28 ngx_http_upstream_t *upstream; | |
29 | |
30 void *data; | |
31 | |
32 ngx_event_get_peer_pt original_get_peer; | |
33 ngx_event_free_peer_pt original_free_peer; | |
34 | |
35 #if (NGX_HTTP_SSL) | |
36 ngx_event_set_peer_session_pt original_set_session; | |
37 ngx_event_save_peer_session_pt original_save_session; | |
38 #endif | |
39 | |
40 ngx_uint_t failed; /* unsigned:1 */ | |
41 | |
42 } ngx_http_upstream_keepalive_peer_data_t; | |
43 | |
44 | |
45 typedef struct { | |
46 ngx_http_upstream_keepalive_srv_conf_t *conf; | |
47 | |
48 ngx_queue_t queue; | |
49 ngx_connection_t *connection; | |
50 | |
51 socklen_t socklen; | |
52 u_char sockaddr[NGX_SOCKADDRLEN]; | |
53 | |
54 } ngx_http_upstream_keepalive_cache_t; | |
55 | |
56 | |
57 static ngx_int_t ngx_http_upstream_init_keepalive_peer(ngx_http_request_t *r, | |
58 ngx_http_upstream_srv_conf_t *us); | |
59 static ngx_int_t ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t *pc, | |
60 void *data); | |
61 static void ngx_http_upstream_free_keepalive_peer(ngx_peer_connection_t *pc, | |
62 void *data, ngx_uint_t state); | |
63 | |
64 static void ngx_http_upstream_keepalive_dummy_handler(ngx_event_t *ev); | |
65 static void ngx_http_upstream_keepalive_close_handler(ngx_event_t *ev); | |
66 static void ngx_http_upstream_keepalive_close(ngx_connection_t *c); | |
67 | |
68 | |
69 #if (NGX_HTTP_SSL) | |
70 static ngx_int_t ngx_http_upstream_keepalive_set_session( | |
71 ngx_peer_connection_t *pc, void *data); | |
72 static void ngx_http_upstream_keepalive_save_session(ngx_peer_connection_t *pc, | |
73 void *data); | |
74 #endif | |
75 | |
76 static void *ngx_http_upstream_keepalive_create_conf(ngx_conf_t *cf); | |
77 static char *ngx_http_upstream_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, | |
78 void *conf); | |
79 | |
80 | |
81 static ngx_command_t ngx_http_upstream_keepalive_commands[] = { | |
82 | |
83 { ngx_string("keepalive"), | |
84 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE12, | |
85 ngx_http_upstream_keepalive, | |
86 0, | |
87 0, | |
88 NULL }, | |
89 | |
90 ngx_null_command | |
91 }; | |
92 | |
93 | |
94 static ngx_http_module_t ngx_http_upstream_keepalive_module_ctx = { | |
95 NULL, /* preconfiguration */ | |
96 NULL, /* postconfiguration */ | |
97 | |
98 NULL, /* create main configuration */ | |
99 NULL, /* init main configuration */ | |
100 | |
101 ngx_http_upstream_keepalive_create_conf, /* create server configuration */ | |
102 NULL, /* merge server configuration */ | |
103 | |
104 NULL, /* create location configuration */ | |
105 NULL /* merge location configuration */ | |
106 }; | |
107 | |
108 | |
109 ngx_module_t ngx_http_upstream_keepalive_module = { | |
110 NGX_MODULE_V1, | |
111 &ngx_http_upstream_keepalive_module_ctx, /* module context */ | |
112 ngx_http_upstream_keepalive_commands, /* module directives */ | |
113 NGX_HTTP_MODULE, /* module type */ | |
114 NULL, /* init master */ | |
115 NULL, /* init module */ | |
116 NULL, /* init process */ | |
117 NULL, /* init thread */ | |
118 NULL, /* exit thread */ | |
119 NULL, /* exit process */ | |
120 NULL, /* exit master */ | |
121 NGX_MODULE_V1_PADDING | |
122 }; | |
123 | |
124 | |
125 static ngx_int_t | |
126 ngx_http_upstream_init_keepalive(ngx_conf_t *cf, | |
127 ngx_http_upstream_srv_conf_t *us) | |
128 { | |
129 ngx_uint_t i; | |
130 ngx_http_upstream_keepalive_srv_conf_t *kcf; | |
131 ngx_http_upstream_keepalive_cache_t *cached; | |
132 | |
133 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, | |
134 "init keepalive"); | |
135 | |
136 kcf = ngx_http_conf_upstream_srv_conf(us, | |
137 ngx_http_upstream_keepalive_module); | |
138 | |
139 if (kcf->original_init_upstream(cf, us) != NGX_OK) { | |
140 return NGX_ERROR; | |
141 } | |
142 | |
143 kcf->original_init_peer = us->peer.init; | |
144 | |
145 us->peer.init = ngx_http_upstream_init_keepalive_peer; | |
146 | |
147 /* allocate cache items and add to free queue */ | |
148 | |
149 cached = ngx_pcalloc(cf->pool, | |
150 sizeof(ngx_http_upstream_keepalive_cache_t) * kcf->max_cached); | |
151 if (cached == NULL) { | |
152 return NGX_ERROR; | |
153 } | |
154 | |
155 ngx_queue_init(&kcf->cache); | |
156 ngx_queue_init(&kcf->free); | |
157 | |
158 for (i = 0; i < kcf->max_cached; i++) { | |
159 ngx_queue_insert_head(&kcf->free, &cached[i].queue); | |
160 cached[i].conf = kcf; | |
161 } | |
162 | |
163 return NGX_OK; | |
164 } | |
165 | |
166 | |
167 static ngx_int_t | |
168 ngx_http_upstream_init_keepalive_peer(ngx_http_request_t *r, | |
169 ngx_http_upstream_srv_conf_t *us) | |
170 { | |
171 ngx_http_upstream_keepalive_peer_data_t *kp; | |
172 ngx_http_upstream_keepalive_srv_conf_t *kcf; | |
173 | |
174 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
175 "init keepalive peer"); | |
176 | |
177 kcf = ngx_http_conf_upstream_srv_conf(us, | |
178 ngx_http_upstream_keepalive_module); | |
179 | |
180 kp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_keepalive_peer_data_t)); | |
181 if (kp == NULL) { | |
182 return NGX_ERROR; | |
183 } | |
184 | |
185 if (kcf->original_init_peer(r, us) != NGX_OK) { | |
186 return NGX_ERROR; | |
187 } | |
188 | |
189 kp->conf = kcf; | |
190 kp->upstream = r->upstream; | |
191 kp->data = r->upstream->peer.data; | |
192 kp->original_get_peer = r->upstream->peer.get; | |
193 kp->original_free_peer = r->upstream->peer.free; | |
194 | |
195 r->upstream->peer.data = kp; | |
196 r->upstream->peer.get = ngx_http_upstream_get_keepalive_peer; | |
197 r->upstream->peer.free = ngx_http_upstream_free_keepalive_peer; | |
198 | |
199 #if (NGX_HTTP_SSL) | |
200 kp->original_set_session = r->upstream->peer.set_session; | |
201 kp->original_save_session = r->upstream->peer.save_session; | |
202 r->upstream->peer.set_session = ngx_http_upstream_keepalive_set_session; | |
203 r->upstream->peer.save_session = ngx_http_upstream_keepalive_save_session; | |
204 #endif | |
205 | |
206 return NGX_OK; | |
207 } | |
208 | |
209 | |
210 static ngx_int_t | |
211 ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t *pc, void *data) | |
212 { | |
213 ngx_http_upstream_keepalive_peer_data_t *kp = data; | |
214 ngx_http_upstream_keepalive_cache_t *item; | |
215 | |
216 ngx_int_t rc; | |
217 ngx_queue_t *q, *cache; | |
218 ngx_connection_t *c; | |
219 | |
220 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
221 "get keepalive peer"); | |
222 | |
223 kp->failed = 0; | |
224 | |
225 /* single pool of cached connections */ | |
226 | |
227 if (kp->conf->single && !ngx_queue_empty(&kp->conf->cache)) { | |
228 | |
229 q = ngx_queue_head(&kp->conf->cache); | |
230 | |
231 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); | |
232 c = item->connection; | |
233 | |
234 ngx_queue_remove(q); | |
235 ngx_queue_insert_head(&kp->conf->free, q); | |
236 | |
237 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
238 "get keepalive peer: using connection %p", c); | |
239 | |
240 c->idle = 0; | |
241 c->log = pc->log; | |
242 c->read->log = pc->log; | |
243 c->write->log = pc->log; | |
244 c->pool->log = pc->log; | |
245 | |
246 pc->connection = c; | |
247 pc->cached = 1; | |
248 | |
249 return NGX_DONE; | |
250 } | |
251 | |
252 rc = kp->original_get_peer(pc, kp->data); | |
253 | |
254 if (kp->conf->single || rc != NGX_OK) { | |
255 return rc; | |
256 } | |
257 | |
258 /* search cache for suitable connection */ | |
259 | |
260 cache = &kp->conf->cache; | |
261 | |
262 for (q = ngx_queue_head(cache); | |
263 q != ngx_queue_sentinel(cache); | |
264 q = ngx_queue_next(q)) | |
265 { | |
266 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); | |
267 c = item->connection; | |
268 | |
269 if (ngx_memn2cmp((u_char *) &item->sockaddr, (u_char *) pc->sockaddr, | |
270 item->socklen, pc->socklen) | |
271 == 0) | |
272 { | |
273 ngx_queue_remove(q); | |
274 ngx_queue_insert_head(&kp->conf->free, q); | |
275 | |
276 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
277 "get keepalive peer: using connection %p", c); | |
278 | |
279 c->idle = 0; | |
280 c->log = pc->log; | |
281 c->read->log = pc->log; | |
282 c->write->log = pc->log; | |
283 c->pool->log = pc->log; | |
284 | |
285 pc->connection = c; | |
286 pc->cached = 1; | |
287 | |
288 return NGX_DONE; | |
289 } | |
290 } | |
291 | |
292 return NGX_OK; | |
293 } | |
294 | |
295 | |
296 static void | |
297 ngx_http_upstream_free_keepalive_peer(ngx_peer_connection_t *pc, void *data, | |
298 ngx_uint_t state) | |
299 { | |
300 ngx_http_upstream_keepalive_peer_data_t *kp = data; | |
301 ngx_http_upstream_keepalive_cache_t *item; | |
302 | |
303 ngx_queue_t *q; | |
304 ngx_connection_t *c; | |
305 ngx_http_upstream_t *u; | |
306 | |
307 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
308 "free keepalive peer"); | |
309 | |
310 /* remember failed state - peer.free() may be called more than once */ | |
311 | |
312 if (state & NGX_PEER_FAILED) { | |
313 kp->failed = 1; | |
314 } | |
315 | |
316 /* cache valid connections */ | |
317 | |
318 u = kp->upstream; | |
319 c = pc->connection; | |
320 | |
321 if (kp->failed | |
322 || c == NULL | |
323 || c->read->eof | |
324 || c->read->error | |
325 || c->read->timedout | |
326 || c->write->error | |
327 || c->write->timedout) | |
328 { | |
329 goto invalid; | |
330 } | |
331 | |
332 if (!u->keepalive) { | |
333 goto invalid; | |
334 } | |
335 | |
336 if (ngx_handle_read_event(c->read, 0) != NGX_OK) { | |
337 goto invalid; | |
338 } | |
339 | |
340 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
341 "free keepalive peer: saving connection %p", c); | |
342 | |
343 if (ngx_queue_empty(&kp->conf->free)) { | |
344 | |
345 q = ngx_queue_last(&kp->conf->cache); | |
346 ngx_queue_remove(q); | |
347 | |
348 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); | |
349 | |
350 ngx_http_upstream_keepalive_close(item->connection); | |
351 | |
352 } else { | |
353 q = ngx_queue_head(&kp->conf->free); | |
354 ngx_queue_remove(q); | |
355 | |
356 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); | |
357 } | |
358 | |
359 item->connection = c; | |
360 ngx_queue_insert_head(&kp->conf->cache, q); | |
361 | |
362 pc->connection = NULL; | |
363 | |
364 if (c->read->timer_set) { | |
365 ngx_del_timer(c->read); | |
366 } | |
367 if (c->write->timer_set) { | |
368 ngx_del_timer(c->write); | |
369 } | |
370 | |
371 c->write->handler = ngx_http_upstream_keepalive_dummy_handler; | |
372 c->read->handler = ngx_http_upstream_keepalive_close_handler; | |
373 | |
374 c->data = item; | |
375 c->idle = 1; | |
376 c->log = ngx_cycle->log; | |
377 c->read->log = ngx_cycle->log; | |
378 c->write->log = ngx_cycle->log; | |
379 c->pool->log = ngx_cycle->log; | |
380 | |
381 item->socklen = pc->socklen; | |
382 ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen); | |
383 | |
384 if (c->read->ready) { | |
385 ngx_http_upstream_keepalive_close_handler(c->read); | |
386 } | |
387 | |
388 invalid: | |
389 | |
390 kp->original_free_peer(pc, kp->data, state); | |
391 } | |
392 | |
393 | |
394 static void | |
395 ngx_http_upstream_keepalive_dummy_handler(ngx_event_t *ev) | |
396 { | |
397 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, | |
398 "keepalive dummy handler"); | |
399 } | |
400 | |
401 | |
402 static void | |
403 ngx_http_upstream_keepalive_close_handler(ngx_event_t *ev) | |
404 { | |
405 ngx_http_upstream_keepalive_srv_conf_t *conf; | |
406 ngx_http_upstream_keepalive_cache_t *item; | |
407 | |
408 int n; | |
409 char buf[1]; | |
410 ngx_connection_t *c; | |
411 | |
412 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, | |
413 "keepalive close handler"); | |
414 | |
415 c = ev->data; | |
416 | |
417 if (c->close) { | |
418 goto close; | |
419 } | |
420 | |
421 n = recv(c->fd, buf, 1, MSG_PEEK); | |
422 | |
423 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) { | |
424 /* stale event */ | |
425 | |
426 if (ngx_handle_read_event(c->read, 0) != NGX_OK) { | |
427 goto close; | |
428 } | |
429 | |
430 return; | |
431 } | |
432 | |
433 close: | |
434 | |
435 item = c->data; | |
436 conf = item->conf; | |
437 | |
438 ngx_http_upstream_keepalive_close(c); | |
439 | |
440 ngx_queue_remove(&item->queue); | |
441 ngx_queue_insert_head(&conf->free, &item->queue); | |
442 } | |
443 | |
444 | |
445 static void | |
446 ngx_http_upstream_keepalive_close(ngx_connection_t *c) | |
447 { | |
448 | |
449 #if (NGX_HTTP_SSL) | |
450 | |
451 if (c->ssl) { | |
452 c->ssl->no_wait_shutdown = 1; | |
453 c->ssl->no_send_shutdown = 1; | |
454 | |
455 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
456 c->ssl->handler = ngx_http_upstream_keepalive_close; | |
457 return; | |
458 } | |
459 } | |
460 | |
461 #endif | |
462 | |
463 ngx_destroy_pool(c->pool); | |
464 ngx_close_connection(c); | |
465 } | |
466 | |
467 | |
468 #if (NGX_HTTP_SSL) | |
469 | |
470 static ngx_int_t | |
471 ngx_http_upstream_keepalive_set_session(ngx_peer_connection_t *pc, void *data) | |
472 { | |
473 ngx_http_upstream_keepalive_peer_data_t *kp = data; | |
474 | |
475 return kp->original_set_session(pc, kp->data); | |
476 } | |
477 | |
478 | |
479 static void | |
480 ngx_http_upstream_keepalive_save_session(ngx_peer_connection_t *pc, void *data) | |
481 { | |
482 ngx_http_upstream_keepalive_peer_data_t *kp = data; | |
483 | |
484 kp->original_save_session(pc, kp->data); | |
485 return; | |
486 } | |
487 | |
488 #endif | |
489 | |
490 | |
491 static void * | |
492 ngx_http_upstream_keepalive_create_conf(ngx_conf_t *cf) | |
493 { | |
494 ngx_http_upstream_keepalive_srv_conf_t *conf; | |
495 | |
496 conf = ngx_pcalloc(cf->pool, | |
497 sizeof(ngx_http_upstream_keepalive_srv_conf_t)); | |
498 if (conf == NULL) { | |
499 return NULL; | |
500 } | |
501 | |
502 /* | |
503 * set by ngx_pcalloc(): | |
504 * | |
505 * conf->original_init_upstream = NULL; | |
506 * conf->original_init_peer = NULL; | |
507 */ | |
508 | |
509 conf->max_cached = 1; | |
510 | |
511 return conf; | |
512 } | |
513 | |
514 | |
515 static char * | |
516 ngx_http_upstream_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
517 { | |
518 ngx_http_upstream_srv_conf_t *uscf; | |
519 ngx_http_upstream_keepalive_srv_conf_t *kcf; | |
520 | |
521 ngx_int_t n; | |
522 ngx_str_t *value; | |
523 ngx_uint_t i; | |
524 | |
525 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); | |
526 | |
527 kcf = ngx_http_conf_upstream_srv_conf(uscf, | |
528 ngx_http_upstream_keepalive_module); | |
529 | |
530 kcf->original_init_upstream = uscf->peer.init_upstream | |
531 ? uscf->peer.init_upstream | |
532 : ngx_http_upstream_init_round_robin; | |
533 | |
534 uscf->peer.init_upstream = ngx_http_upstream_init_keepalive; | |
535 | |
536 /* read options */ | |
537 | |
538 value = cf->args->elts; | |
539 | |
540 n = ngx_atoi(value[1].data, value[1].len); | |
541 | |
542 if (n == NGX_ERROR || n == 0) { | |
543 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
544 "invalid value \"%V\" in \"%V\" directive", | |
545 &value[1], &cmd->name); | |
546 return NGX_CONF_ERROR; | |
547 } | |
548 | |
549 kcf->max_cached = n; | |
550 | |
551 for (i = 2; i < cf->args->nelts; i++) { | |
552 | |
553 if (ngx_strcmp(value[i].data, "single") == 0) { | |
554 kcf->single = 1; | |
555 continue; | |
556 } | |
557 | |
558 goto invalid; | |
559 } | |
560 | |
561 return NGX_CONF_OK; | |
562 | |
563 invalid: | |
564 | |
565 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
566 "invalid parameter \"%V\"", &value[i]); | |
567 | |
568 return NGX_CONF_ERROR; | |
569 } |