comparison src/http/ngx_http_request.c @ 7006:9552758a786e

Background subrequests for cache updates. Previously, cache background update might not work as expected, making client wait for it to complete before receiving the final part of a stale response. This could happen if the response could not be sent to the client socket in one filter chain call. Now background cache update is done in a background subrequest. This type of subrequest does not block any other subrequests or the main request.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 25 May 2017 15:57:59 +0300
parents 8801ff7d58e1
children ed1101bbf19f
comparison
equal deleted inserted replaced
7005:3e2d90073adf 7006:9552758a786e
2355 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc)); 2355 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
2356 return; 2356 return;
2357 } 2357 }
2358 2358
2359 if (r != r->main) { 2359 if (r != r->main) {
2360 2360 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2361 if (r->buffered || r->postponed) { 2361
2362 2362 if (r->background) {
2363 if (ngx_http_set_write_handler(r) != NGX_OK) {
2364 ngx_http_terminate_request(r, 0);
2365 }
2366
2367 return;
2368 }
2369
2370 pr = r->parent;
2371
2372 if (r == c->data) {
2373
2374 r->main->count--;
2375
2376 if (!r->logged) { 2363 if (!r->logged) {
2377
2378 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2379
2380 if (clcf->log_subrequest) { 2364 if (clcf->log_subrequest) {
2381 ngx_http_log_request(r); 2365 ngx_http_log_request(r);
2382 } 2366 }
2383 2367
2384 r->logged = 1; 2368 r->logged = 1;
2388 "subrequest: \"%V?%V\" logged again", 2372 "subrequest: \"%V?%V\" logged again",
2389 &r->uri, &r->args); 2373 &r->uri, &r->args);
2390 } 2374 }
2391 2375
2392 r->done = 1; 2376 r->done = 1;
2377 ngx_http_finalize_connection(r);
2378 return;
2379 }
2380
2381 if (r->buffered || r->postponed) {
2382
2383 if (ngx_http_set_write_handler(r) != NGX_OK) {
2384 ngx_http_terminate_request(r, 0);
2385 }
2386
2387 return;
2388 }
2389
2390 pr = r->parent;
2391
2392 if (r == c->data) {
2393
2394 r->main->count--;
2395
2396 if (!r->logged) {
2397 if (clcf->log_subrequest) {
2398 ngx_http_log_request(r);
2399 }
2400
2401 r->logged = 1;
2402
2403 } else {
2404 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
2405 "subrequest: \"%V?%V\" logged again",
2406 &r->uri, &r->args);
2407 }
2408
2409 r->done = 1;
2393 2410
2394 if (pr->postponed && pr->postponed->request == r) { 2411 if (pr->postponed && pr->postponed->request == r) {
2395 pr->postponed = pr->postponed->next; 2412 pr->postponed = pr->postponed->next;
2396 } 2413 }
2397 2414
2438 &r->uri, &r->args); 2455 &r->uri, &r->args);
2439 return; 2456 return;
2440 } 2457 }
2441 2458
2442 r->done = 1; 2459 r->done = 1;
2460
2461 r->read_event_handler = ngx_http_block_reading;
2443 r->write_event_handler = ngx_http_request_empty_handler; 2462 r->write_event_handler = ngx_http_request_empty_handler;
2444 2463
2445 if (!r->post_action) { 2464 if (!r->post_action) {
2446 r->request_complete = 1; 2465 r->request_complete = 1;
2447 } 2466 }
2555 } 2574 }
2556 2575
2557 ngx_http_close_request(r, 0); 2576 ngx_http_close_request(r, 0);
2558 return; 2577 return;
2559 } 2578 }
2579
2580 r = r->main;
2560 2581
2561 if (r->reading_body) { 2582 if (r->reading_body) {
2562 r->keepalive = 0; 2583 r->keepalive = 0;
2563 r->lingering_close = 1; 2584 r->lingering_close = 1;
2564 } 2585 }