[PATCH 07 of 15] Tempfiles: Wait handlers

Jiří Setnička jiri.setnicka at cdn77.com
Fri Jan 28 16:31:59 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 10e917f6ddb56c338f9597b76a68cdbf21d8f8e8
# Parent  5e3013a56643a9f8d26ea0e5882a1aa986c51903
Tempfiles: Wait handlers
First commit in sequence of commits which adds ability to serve multiple
requests on-the-fly from the one growing tempfile.

diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -16,6 +16,9 @@ static ngx_int_t ngx_http_file_cache_loc
 static void ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev);
 static void ngx_http_file_cache_lock_wait(ngx_http_request_t *r,
     ngx_http_cache_t *c);
+static void ngx_http_file_cache_tempfile_wait_handler(ngx_event_t *ev);
+static ngx_int_t ngx_http_file_cache_wait_for_temp_file(ngx_http_request_t *r,
+    ngx_http_cache_t *c);
 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
     ngx_http_cache_t *c);
 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r,
@@ -622,6 +625,86 @@ wakeup:
 }
 
 
+static void
+ngx_http_file_cache_tempfile_wait_handler(ngx_event_t *ev)
+{
+    ngx_connection_t    *c;
+    ngx_http_request_t  *r;
+
+    r = ev->data;
+    c = r->connection;
+
+    ngx_http_set_log_request(c->log, r);
+
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                   "http file cache wait: \"%V?%V\"", &r->uri, &r->args);
+
+    r->main->blocked--;
+    r->cache_tempfile_busy = 0;
+
+    r->write_event_handler(r);
+
+    ngx_http_run_posted_requests(c);
+}
+
+
+static ngx_int_t
+ngx_http_file_cache_wait_for_temp_file(ngx_http_request_t *r, ngx_http_cache_t *c)
+{
+    ngx_int_t               updated, done;
+    ngx_uint_t              wait;
+    ngx_msec_t              now, timer;
+    ngx_http_file_cache_t  *cache;
+
+    c->waiting = 0;
+
+    now = ngx_current_msec;
+
+    timer = c->wait_time - now;
+
+    if ((ngx_msec_int_t) timer <= 0) {
+        ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
+                      "cache tempfile lock timeout");
+
+        c->tempfile_timeout = 0;
+
+        return NGX_HTTP_GATEWAY_TIME_OUT;
+    }
+
+    cache = c->file_cache;
+    wait = 0;
+
+    ngx_shmtx_lock(&cache->shpool->mutex);
+
+    updated = c->tf_node ? c->tf_node->updated : 0;
+    done = c->tf_node ? c->tf_node->done : 0;
+
+    if (!done && c->node->updating) {
+        wait = 1;
+    }
+
+    ngx_shmtx_unlock(&cache->shpool->mutex);
+
+    if (updated) {
+        return NGX_DONE;
+    }
+
+    if (wait) {
+        c->waiting = 1;
+
+        ngx_add_timer(&c->wait_event,
+                      (timer > c->tempfile_loop) ? c->tempfile_loop : timer);
+
+        r->main->blocked++;
+        r->cache_tempfile_busy = 1;
+
+        return NGX_AGAIN;
+    }
+
+    return NGX_DECLINED;
+}
+
+
 static ngx_int_t
 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
 {
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -556,6 +556,11 @@ struct ngx_http_request_s {
     unsigned                          subrequest_ranges:1;
     unsigned                          single_range:1;
     unsigned                          disable_not_modified:1;
+
+#if (NGX_HTTP_CACHE)
+    unsigned                          cache_tempfile_busy:1;
+#endif
+
     unsigned                          stat_reading:1;
     unsigned                          stat_writing:1;
     unsigned                          stat_processing:1;
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
@@ -561,6 +561,10 @@ ngx_http_upstream_init_request(ngx_http_
         return;
     }
 
+    if (r->cache_tempfile_busy) {
+        return;
+    }
+
     u = r->upstream;
 
 #if (NGX_HTTP_CACHE)


More information about the nginx-devel mailing list