comparison src/http/modules/ngx_http_upstream_keepalive_module.c @ 6167:87d48f8793b6

Upstream keepalive: reduced diffs to the plus version of nginx.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 04 Jun 2015 23:46:44 +0300
parents 4d8936b1fc32
children 5e6142609e48
comparison
equal deleted inserted replaced
6166:ac52b46ebab5 6167:87d48f8793b6
23 23
24 24
25 typedef struct { 25 typedef struct {
26 ngx_http_upstream_keepalive_srv_conf_t *conf; 26 ngx_http_upstream_keepalive_srv_conf_t *conf;
27 27
28 ngx_queue_t queue;
29 ngx_connection_t *connection;
30
31 socklen_t socklen;
32 u_char sockaddr[NGX_SOCKADDRLEN];
33
34 } ngx_http_upstream_keepalive_cache_t;
35
36
37 typedef struct {
38 ngx_http_upstream_keepalive_srv_conf_t *conf;
39
28 ngx_http_upstream_t *upstream; 40 ngx_http_upstream_t *upstream;
29 41
30 void *data; 42 void *data;
31 43
32 ngx_event_get_peer_pt original_get_peer; 44 ngx_event_get_peer_pt original_get_peer;
36 ngx_event_set_peer_session_pt original_set_session; 48 ngx_event_set_peer_session_pt original_set_session;
37 ngx_event_save_peer_session_pt original_save_session; 49 ngx_event_save_peer_session_pt original_save_session;
38 #endif 50 #endif
39 51
40 } ngx_http_upstream_keepalive_peer_data_t; 52 } ngx_http_upstream_keepalive_peer_data_t;
41
42
43 typedef struct {
44 ngx_http_upstream_keepalive_srv_conf_t *conf;
45
46 ngx_queue_t queue;
47 ngx_connection_t *connection;
48
49 socklen_t socklen;
50 u_char sockaddr[NGX_SOCKADDRLEN];
51
52 } ngx_http_upstream_keepalive_cache_t;
53 53
54 54
55 static ngx_int_t ngx_http_upstream_init_keepalive_peer(ngx_http_request_t *r, 55 static ngx_int_t ngx_http_upstream_init_keepalive_peer(ngx_http_request_t *r,
56 ngx_http_upstream_srv_conf_t *us); 56 ngx_http_upstream_srv_conf_t *us);
57 static ngx_int_t ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t *pc, 57 static ngx_int_t ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t *pc,
60 void *data, ngx_uint_t state); 60 void *data, ngx_uint_t state);
61 61
62 static void ngx_http_upstream_keepalive_dummy_handler(ngx_event_t *ev); 62 static void ngx_http_upstream_keepalive_dummy_handler(ngx_event_t *ev);
63 static void ngx_http_upstream_keepalive_close_handler(ngx_event_t *ev); 63 static void ngx_http_upstream_keepalive_close_handler(ngx_event_t *ev);
64 static void ngx_http_upstream_keepalive_close(ngx_connection_t *c); 64 static void ngx_http_upstream_keepalive_close(ngx_connection_t *c);
65
66 65
67 #if (NGX_HTTP_SSL) 66 #if (NGX_HTTP_SSL)
68 static ngx_int_t ngx_http_upstream_keepalive_set_session( 67 static ngx_int_t ngx_http_upstream_keepalive_set_session(
69 ngx_peer_connection_t *pc, void *data); 68 ngx_peer_connection_t *pc, void *data);
70 static void ngx_http_upstream_keepalive_save_session(ngx_peer_connection_t *pc, 69 static void ngx_http_upstream_keepalive_save_session(ngx_peer_connection_t *pc,
242 == 0) 241 == 0)
243 { 242 {
244 ngx_queue_remove(q); 243 ngx_queue_remove(q);
245 ngx_queue_insert_head(&kp->conf->free, q); 244 ngx_queue_insert_head(&kp->conf->free, q);
246 245
247 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, 246 goto found;
248 "get keepalive peer: using connection %p", c);
249
250 c->idle = 0;
251 c->sent = 0;
252 c->log = pc->log;
253 c->read->log = pc->log;
254 c->write->log = pc->log;
255 c->pool->log = pc->log;
256
257 pc->connection = c;
258 pc->cached = 1;
259
260 return NGX_DONE;
261 } 247 }
262 } 248 }
263 249
264 return NGX_OK; 250 return NGX_OK;
251
252 found:
253
254 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
255 "get keepalive peer: using connection %p", c);
256
257 c->idle = 0;
258 c->sent = 0;
259 c->log = pc->log;
260 c->read->log = pc->log;
261 c->write->log = pc->log;
262 c->pool->log = pc->log;
263
264 pc->connection = c;
265 pc->cached = 1;
266
267 return NGX_DONE;
265 } 268 }
266 269
267 270
268 static void 271 static void
269 ngx_http_upstream_free_keepalive_peer(ngx_peer_connection_t *pc, void *data, 272 ngx_http_upstream_free_keepalive_peer(ngx_peer_connection_t *pc, void *data,
320 ngx_queue_remove(q); 323 ngx_queue_remove(q);
321 324
322 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); 325 item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue);
323 } 326 }
324 327
328 ngx_queue_insert_head(&kp->conf->cache, q);
329
325 item->connection = c; 330 item->connection = c;
326 ngx_queue_insert_head(&kp->conf->cache, q);
327 331
328 pc->connection = NULL; 332 pc->connection = NULL;
329 333
330 if (c->read->timer_set) { 334 if (c->read->timer_set) {
331 ngx_del_timer(c->read); 335 ngx_del_timer(c->read);
468 /* 472 /*
469 * set by ngx_pcalloc(): 473 * set by ngx_pcalloc():
470 * 474 *
471 * conf->original_init_upstream = NULL; 475 * conf->original_init_upstream = NULL;
472 * conf->original_init_peer = NULL; 476 * conf->original_init_peer = NULL;
477 * conf->max_cached = 0;
473 */ 478 */
474
475 conf->max_cached = 1;
476 479
477 return conf; 480 return conf;
478 } 481 }
479 482
480 483
485 ngx_http_upstream_keepalive_srv_conf_t *kcf = conf; 488 ngx_http_upstream_keepalive_srv_conf_t *kcf = conf;
486 489
487 ngx_int_t n; 490 ngx_int_t n;
488 ngx_str_t *value; 491 ngx_str_t *value;
489 492
490 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); 493 if (kcf->max_cached) {
491
492 if (kcf->original_init_upstream) {
493 return "is duplicate"; 494 return "is duplicate";
494 } 495 }
495
496 kcf->original_init_upstream = uscf->peer.init_upstream
497 ? uscf->peer.init_upstream
498 : ngx_http_upstream_init_round_robin;
499
500 uscf->peer.init_upstream = ngx_http_upstream_init_keepalive;
501 496
502 /* read options */ 497 /* read options */
503 498
504 value = cf->args->elts; 499 value = cf->args->elts;
505 500
512 return NGX_CONF_ERROR; 507 return NGX_CONF_ERROR;
513 } 508 }
514 509
515 kcf->max_cached = n; 510 kcf->max_cached = n;
516 511
512 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
513
514 kcf->original_init_upstream = uscf->peer.init_upstream
515 ? uscf->peer.init_upstream
516 : ngx_http_upstream_init_round_robin;
517
518 uscf->peer.init_upstream = ngx_http_upstream_init_keepalive;
519
517 return NGX_CONF_OK; 520 return NGX_CONF_OK;
518 } 521 }