diff 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
line wrap: on
line diff
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2357,6 +2357,26 @@ ngx_http_finalize_request(ngx_http_reque
     }
 
     if (r != r->main) {
+        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+        if (r->background) {
+            if (!r->logged) {
+                if (clcf->log_subrequest) {
+                    ngx_http_log_request(r);
+                }
+
+                r->logged = 1;
+
+            } else {
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "subrequest: \"%V?%V\" logged again",
+                              &r->uri, &r->args);
+            }
+
+            r->done = 1;
+            ngx_http_finalize_connection(r);
+            return;
+        }
 
         if (r->buffered || r->postponed) {
 
@@ -2374,9 +2394,6 @@ ngx_http_finalize_request(ngx_http_reque
             r->main->count--;
 
             if (!r->logged) {
-
-                clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
                 if (clcf->log_subrequest) {
                     ngx_http_log_request(r);
                 }
@@ -2440,6 +2457,8 @@ ngx_http_finalize_request(ngx_http_reque
     }
 
     r->done = 1;
+
+    r->read_event_handler = ngx_http_block_reading;
     r->write_event_handler = ngx_http_request_empty_handler;
 
     if (!r->post_action) {
@@ -2558,6 +2577,8 @@ ngx_http_finalize_connection(ngx_http_re
         return;
     }
 
+    r = r->main;
+
     if (r->reading_body) {
         r->keepalive = 0;
         r->lingering_close = 1;