[PATCH 10 of 15] Tempfiles: Setup event handlers in ngx_http_upstream.c

Jiří Setnička jiri.setnicka at cdn77.com
Fri Jan 28 16:32:02 UTC 2022


# HG changeset patch
# User Jiří Setnička  <jiri.setnicka at cdn77.com>
# Date 1643385660 -3600
#      Fri Jan 28 17:01:00 2022 +0100
# Node ID bd12e8ba1af2005260e68a410e3c8927a88dac1a
# Parent  24453fd1ce204f361748c32e3c271d9e6fc7c9eb
Tempfiles: Setup event handlers in ngx_http_upstream.c
Introduced in the previous commits the file cache could serve multiple
clients from the currently downloading tempfiles. This is achieved by
repeated execution of ngx_http_file_cache_send and related functions.

Each run is performed by executing ngx_http_upstream_init_request,
invoked by either write event or by tempfiles timer.

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -576,10 +576,12 @@ ngx_http_upstream_init_request(ngx_http_
 
         if (rc == NGX_BUSY) {
             r->write_event_handler = ngx_http_upstream_init_request;
+            r->read_event_handler = ngx_http_test_reading;
             return;
         }
 
         r->write_event_handler = ngx_http_request_empty_handler;
+        r->read_event_handler = ngx_http_block_reading;
 
         if (rc == NGX_ERROR) {
             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
@@ -589,6 +591,15 @@ ngx_http_upstream_init_request(ngx_http_
         if (rc == NGX_OK) {
             rc = ngx_http_upstream_cache_send(r, u);
 
+            if (rc == NGX_BUSY) {
+                r->write_event_handler = ngx_http_upstream_init_request;
+                r->read_event_handler = ngx_http_test_reading;
+                return;
+            }
+
+            r->write_event_handler = ngx_http_request_empty_handler;
+            r->read_event_handler = ngx_http_block_reading;
+
             if (rc == NGX_DONE) {
                 return;
             }
@@ -1088,6 +1099,10 @@ ngx_http_upstream_cache_send(ngx_http_re
         return NGX_ERROR;
     }
 
+    if (r->header_sent) {
+        return ngx_http_cache_send(r);
+    }
+
     rc = u->process_header(r);
 
     if (rc == NGX_OK) {
@@ -2984,7 +2999,11 @@ ngx_http_upstream_send_response(ngx_http
     ngx_connection_t          *c;
     ngx_http_core_loc_conf_t  *clcf;
 
-    rc = ngx_http_send_header(r);
+    if (r->header_sent) {
+        rc = NGX_OK;
+    } else {
+        rc = ngx_http_send_header(r);
+    }
 
     if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) {
         ngx_http_upstream_finalize_request(r, u, rc);


More information about the nginx-devel mailing list