Klaus Demo nginx / 02ce6c4
Upstream: limited next_upstream time and tries (ticket #544). The new directives {proxy,fastcgi,scgi,uwsgi,memcached}_next_upstream_tries and {proxy,fastcgi,scgi,uwsgi,memcached}_next_upstream_timeout limit the number of upstreams tried and the maximum time spent for these tries when searching for a valid upstream. Roman Arutyunyan 7 years ago
8 changed file(s) with 128 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
4141 ngx_str_t *name;
4242
4343 ngx_uint_t tries;
44 ngx_msec_t start_time;
4445
4546 ngx_event_get_peer_pt get;
4647 ngx_event_free_peer_pt free;
441441 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream),
442442 &ngx_http_fastcgi_next_upstream_masks },
443443
444 { ngx_string("fastcgi_next_upstream_tries"),
445 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
446 ngx_conf_set_num_slot,
447 NGX_HTTP_LOC_CONF_OFFSET,
448 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream_tries),
449 NULL },
450
451 { ngx_string("fastcgi_next_upstream_timeout"),
452 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
453 ngx_conf_set_msec_slot,
454 NGX_HTTP_LOC_CONF_OFFSET,
455 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream_timeout),
456 NULL },
457
444458 { ngx_string("fastcgi_param"),
445459 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
446460 ngx_http_upstream_param_set_slot,
23132327
23142328 conf->upstream.store = NGX_CONF_UNSET;
23152329 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
2330 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
23162331 conf->upstream.buffering = NGX_CONF_UNSET;
23172332 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
23182333
23212336 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
23222337 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
23232338 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
2339 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
23242340
23252341 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
23262342 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
23862402 ngx_conf_merge_uint_value(conf->upstream.store_access,
23872403 prev->upstream.store_access, 0600);
23882404
2405 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
2406 prev->upstream.next_upstream_tries, 0);
2407
23892408 ngx_conf_merge_value(conf->upstream.buffering,
23902409 prev->upstream.buffering, 1);
23912410
24032422
24042423 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
24052424 prev->upstream.read_timeout, 60000);
2425
2426 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
2427 prev->upstream.next_upstream_timeout, 0);
24062428
24072429 ngx_conf_merge_size_value(conf->upstream.send_lowat,
24082430 prev->upstream.send_lowat, 0);
100100 NGX_HTTP_LOC_CONF_OFFSET,
101101 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream),
102102 &ngx_http_memcached_next_upstream_masks },
103
104 { ngx_string("memcached_next_upstream_tries"),
105 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
106 ngx_conf_set_num_slot,
107 NGX_HTTP_LOC_CONF_OFFSET,
108 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream_tries),
109 NULL },
110
111 { ngx_string("memcached_next_upstream_timeout"),
112 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
113 ngx_conf_set_msec_slot,
114 NGX_HTTP_LOC_CONF_OFFSET,
115 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream_timeout),
116 NULL },
103117
104118 { ngx_string("memcached_gzip_flag"),
105119 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
582596 */
583597
584598 conf->upstream.local = NGX_CONF_UNSET_PTR;
599 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
585600 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
586601 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
587602 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
603 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
588604
589605 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
590606
618634 ngx_conf_merge_ptr_value(conf->upstream.local,
619635 prev->upstream.local, NULL);
620636
637 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
638 prev->upstream.next_upstream_tries, 0);
639
621640 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
622641 prev->upstream.connect_timeout, 60000);
623642
626645
627646 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
628647 prev->upstream.read_timeout, 60000);
648
649 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
650 prev->upstream.next_upstream_timeout, 0);
629651
630652 ngx_conf_merge_size_value(conf->upstream.buffer_size,
631653 prev->upstream.buffer_size,
503503 NGX_HTTP_LOC_CONF_OFFSET,
504504 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
505505 &ngx_http_proxy_next_upstream_masks },
506
507 { ngx_string("proxy_next_upstream_tries"),
508 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
509 ngx_conf_set_num_slot,
510 NGX_HTTP_LOC_CONF_OFFSET,
511 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream_tries),
512 NULL },
513
514 { ngx_string("proxy_next_upstream_timeout"),
515 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
516 ngx_conf_set_msec_slot,
517 NGX_HTTP_LOC_CONF_OFFSET,
518 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream_timeout),
519 NULL },
506520
507521 { ngx_string("proxy_pass_header"),
508522 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
24542468
24552469 conf->upstream.store = NGX_CONF_UNSET;
24562470 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
2471 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
24572472 conf->upstream.buffering = NGX_CONF_UNSET;
24582473 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
24592474
24622477 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
24632478 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
24642479 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
2480 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
24652481
24662482 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
24672483 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
25422558 ngx_conf_merge_uint_value(conf->upstream.store_access,
25432559 prev->upstream.store_access, 0600);
25442560
2561 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
2562 prev->upstream.next_upstream_tries, 0);
2563
25452564 ngx_conf_merge_value(conf->upstream.buffering,
25462565 prev->upstream.buffering, 1);
25472566
25592578
25602579 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
25612580 prev->upstream.read_timeout, 60000);
2581
2582 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
2583 prev->upstream.next_upstream_timeout, 0);
25622584
25632585 ngx_conf_merge_size_value(conf->upstream.send_lowat,
25642586 prev->upstream.send_lowat, 0);
297297 NGX_HTTP_LOC_CONF_OFFSET,
298298 offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream),
299299 &ngx_http_scgi_next_upstream_masks },
300
301 { ngx_string("scgi_next_upstream_tries"),
302 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
303 ngx_conf_set_num_slot,
304 NGX_HTTP_LOC_CONF_OFFSET,
305 offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream_tries),
306 NULL },
307
308 { ngx_string("scgi_next_upstream_timeout"),
309 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
310 ngx_conf_set_msec_slot,
311 NGX_HTTP_LOC_CONF_OFFSET,
312 offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream_timeout),
313 NULL },
300314
301315 { ngx_string("scgi_param"),
302316 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
10731087
10741088 conf->upstream.store = NGX_CONF_UNSET;
10751089 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1090 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
10761091 conf->upstream.buffering = NGX_CONF_UNSET;
10771092 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
10781093
10811096 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
10821097 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
10831098 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1099 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
10841100
10851101 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
10861102 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
11411157 ngx_conf_merge_uint_value(conf->upstream.store_access,
11421158 prev->upstream.store_access, 0600);
11431159
1160 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
1161 prev->upstream.next_upstream_tries, 0);
1162
11441163 ngx_conf_merge_value(conf->upstream.buffering,
11451164 prev->upstream.buffering, 1);
11461165
11581177
11591178 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
11601179 prev->upstream.read_timeout, 60000);
1180
1181 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
1182 prev->upstream.next_upstream_timeout, 0);
11611183
11621184 ngx_conf_merge_size_value(conf->upstream.send_lowat,
11631185 prev->upstream.send_lowat, 0);
352352 NGX_HTTP_LOC_CONF_OFFSET,
353353 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream),
354354 &ngx_http_uwsgi_next_upstream_masks },
355
356 { ngx_string("uwsgi_next_upstream_tries"),
357 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
358 ngx_conf_set_num_slot,
359 NGX_HTTP_LOC_CONF_OFFSET,
360 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_tries),
361 NULL },
362
363 { ngx_string("uwsgi_next_upstream_timeout"),
364 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
365 ngx_conf_set_msec_slot,
366 NGX_HTTP_LOC_CONF_OFFSET,
367 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_timeout),
368 NULL },
355369
356370 { ngx_string("uwsgi_param"),
357371 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
12531267
12541268 conf->upstream.store = NGX_CONF_UNSET;
12551269 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1270 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
12561271 conf->upstream.buffering = NGX_CONF_UNSET;
12571272 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
12581273
12611276 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
12621277 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
12631278 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1279 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
12641280
12651281 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
12661282 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
13281344 ngx_conf_merge_uint_value(conf->upstream.store_access,
13291345 prev->upstream.store_access, 0600);
13301346
1347 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
1348 prev->upstream.next_upstream_tries, 0);
1349
13311350 ngx_conf_merge_value(conf->upstream.buffering,
13321351 prev->upstream.buffering, 1);
13331352
13451364
13461365 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
13471366 prev->upstream.read_timeout, 60000);
1367
1368 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
1369 prev->upstream.next_upstream_timeout, 0);
13481370
13491371 ngx_conf_merge_size_value(conf->upstream.send_lowat,
13501372 prev->upstream.send_lowat, 0);
697697 return;
698698 }
699699
700 u->peer.start_time = ngx_current_msec;
701
702 if (u->conf->next_upstream_tries
703 && u->peer.tries > u->conf->next_upstream_tries)
704 {
705 u->peer.tries = u->conf->next_upstream_tries;
706 }
707
700708 ngx_http_upstream_connect(r, u);
701709 }
702710
34203428 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
34213429 ngx_uint_t ft_type)
34223430 {
3431 ngx_msec_t timeout;
34233432 ngx_uint_t status, state;
34243433
34253434 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
34893498
34903499 if (status) {
34913500 u->state->status = status;
3492
3493 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
3494
3501 timeout = u->conf->next_upstream_timeout;
3502
3503 if (u->peer.tries == 0
3504 || !(u->conf->next_upstream & ft_type)
3505 || (timeout && ngx_current_msec - u->peer.start_time >= timeout))
3506 {
34953507 #if (NGX_HTTP_CACHE)
34963508
34973509 if (u->cache_status == NGX_HTTP_CACHE_EXPIRED
135135 ngx_msec_t send_timeout;
136136 ngx_msec_t read_timeout;
137137 ngx_msec_t timeout;
138 ngx_msec_t next_upstream_timeout;
138139
139140 size_t send_lowat;
140141 size_t buffer_size;
152153 ngx_uint_t ignore_headers;
153154 ngx_uint_t next_upstream;
154155 ngx_uint_t store_access;
156 ngx_uint_t next_upstream_tries;
155157 ngx_flag_t buffering;
156158 ngx_flag_t pass_request_headers;
157159 ngx_flag_t pass_request_body;