[PATCH 05 of 15] http upstream & file_cache: store temp file number and length in tf_node

Jiří Setnička jiri.setnicka at cdn77.com
Fri Jan 28 16:31:57 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 76c1a836b1de47cb16b636b585526e25574e0f58
# Parent  2488cf77a1cc6c7f48696816f085c71e49355b72
http upstream & file_cache: store temp file number and length in tf_node
Number could be used to construct name of the tempfile, length is used
by secondary requests to read the tempfile.

diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -59,6 +59,7 @@ typedef struct {
     size_t                           body_start;
     off_t                            fs_size;
     ngx_msec_t                       lock_time;
+    uint32_t                         tf_number; /* 0 = no temp file exists */
 } ngx_http_file_cache_node_t;
 
 typedef struct {
@@ -211,6 +212,8 @@ struct ngx_http_file_cache_s {
 ngx_int_t ngx_http_file_cache_new(ngx_http_request_t *r);
 ngx_int_t ngx_http_file_cache_create(ngx_http_request_t *r);
 void ngx_http_file_cache_create_key(ngx_http_request_t *r);
+void ngx_http_file_cache_update_tf(ngx_http_request_t *r,
+    ngx_http_cache_t *c, uint32_t tf_number, off_t length);
 ngx_int_t ngx_http_file_cache_open(ngx_http_request_t *r);
 ngx_int_t ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
 void ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf);
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
@@ -319,6 +319,45 @@ ngx_http_file_cache_create_key(ngx_http_
     ngx_memcpy(c->main, c->key, NGX_HTTP_CACHE_KEY_LEN);
 }
 
+void
+ngx_http_file_cache_update_tf(ngx_http_request_t *r, ngx_http_cache_t *c,
+    uint32_t tf_number, off_t length)
+{
+    ngx_http_file_cache_t  *cache;
+
+    if (!c->node) {
+        return;
+    }
+
+    cache = c->file_cache;
+
+    ngx_shmtx_lock(&cache->shpool->mutex);
+
+    if (c->tf_node == NULL) {
+        c->node->tf_number = tf_number;
+        c->tf_node = ngx_slab_calloc_locked(cache->tf_shpool,
+                                            sizeof(ngx_http_file_cache_tf_node_t));
+        if (c->tf_node == NULL) {
+            ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
+                        "could not allocate tf node%s", cache->shpool->log_ctx);
+            return;
+        }
+
+        cache->tf_sh->count++;
+
+        c->tf_node->node.key = tf_number;
+
+        ngx_rbtree_insert(&cache->tf_sh->rbtree, &c->tf_node->node);
+
+        c->tf_node->updated = 0;
+        c->tf_node->count = 1;
+    }
+
+    c->tf_node->length = length;
+
+    ngx_shmtx_unlock(&cache->shpool->mutex);
+}
+
 
 ngx_int_t
 ngx_http_file_cache_open(ngx_http_request_t *r)
@@ -971,6 +1010,7 @@ renew:
     fcn->uniq = 0;
     fcn->body_start = 0;
     fcn->fs_size = 0;
+    fcn->tf_number = 0;
 
 done:
 
@@ -1547,6 +1587,7 @@ ngx_http_file_cache_update(ngx_http_requ
     }
 
     c->node->updating = 0;
+    c->node->tf_number = 0;
 
     ngx_shmtx_unlock(&cache->shpool->mutex);
 }
@@ -1759,6 +1800,7 @@ ngx_http_file_cache_free(ngx_http_cache_
 
     if (c->updating && fcn->lock_time == c->lock_time) {
         fcn->updating = 0;
+        fcn->tf_number = 0;
     }
 
     if (c->tf_node != NULL) {
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
@@ -4069,6 +4069,14 @@ ngx_http_upstream_process_upstream(ngx_h
             ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
             return;
         }
+
+#if (NGX_HTTP_CACHE)
+        if (u->cacheable && r->cache) {
+            ngx_http_file_cache_update_tf(r, r->cache,
+                                          p->temp_file->suffix_number,
+                                          p->temp_file->offset);
+        }
+#endif
     }
 
     ngx_http_upstream_process_request(r, u);


More information about the nginx-devel mailing list