[PATCH 04 of 15] Cache: tf_node for tracking opened tempfile

Jiří Setnička jiri.setnicka at cdn77.com
Fri Jan 28 16:31:56 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 2488cf77a1cc6c7f48696816f085c71e49355b72
# Parent  535e503156cf141bf9471895468423e82f68c8bb
Cache: tf_node for tracking opened tempfile
Lookup and cleanup.

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
@@ -61,6 +61,14 @@ typedef struct {
     ngx_msec_t                       lock_time;
 } ngx_http_file_cache_node_t;
 
+typedef struct {
+    ngx_rbtree_node_t                node;
+    off_t                            length;
+    unsigned                         count:20;
+    unsigned                         updated:1;
+    unsigned                         done:1;
+} ngx_http_file_cache_tf_node_t;
+
 
 struct ngx_http_cache_s {
     ngx_file_t                       file;
@@ -91,6 +99,8 @@ struct ngx_http_cache_s {
     ngx_uint_t                       valid_msec;
     ngx_uint_t                       vary_tag;
 
+    ngx_http_file_cache_tf_node_t   *tf_node;
+
     ngx_buf_t                       *buf;
 
     ngx_http_file_cache_t           *file_cache;
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
@@ -34,6 +34,10 @@ static ngx_int_t ngx_http_file_cache_nam
     ngx_path_t *path);
 static ngx_http_file_cache_node_t *
     ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
+static ngx_http_file_cache_tf_node_t * ngx_http_file_cache_tf_lookup(
+    ngx_http_file_cache_t *cache, uint32_t tf_number);
+static void ngx_http_file_cache_tf_delete(ngx_http_cache_t *c,
+    ngx_http_file_cache_t *cache);
 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
 static void ngx_http_file_cache_vary(ngx_http_request_t *r, u_char *vary,
@@ -1066,6 +1070,53 @@ ngx_http_file_cache_lookup(ngx_http_file
 }
 
 
+static ngx_http_file_cache_tf_node_t *
+ngx_http_file_cache_tf_lookup(ngx_http_file_cache_t *cache, uint32_t tf_number)
+{
+    ngx_rbtree_key_t    node_key;
+    ngx_rbtree_node_t  *node, *sentinel;
+
+    node_key = tf_number;
+
+    node = cache->tf_sh->rbtree.root;
+    sentinel = cache->tf_sh->rbtree.sentinel;
+
+    while (node != sentinel) {
+
+        if (node_key < node->key) {
+            node = node->left;
+            continue;
+        }
+
+        if (node_key > node->key) {
+            node = node->right;
+            continue;
+        }
+
+        return (ngx_http_file_cache_tf_node_t *) node;
+    }
+
+    /* not found */
+
+    return NULL;
+}
+
+
+static void
+ngx_http_file_cache_tf_delete(ngx_http_cache_t *c, ngx_http_file_cache_t *cache)
+{
+    c->tf_node->count--;
+
+    if (c->tf_node->count == 0) {
+        ngx_rbtree_delete(&cache->tf_sh->rbtree, &c->tf_node->node);
+        ngx_slab_free_locked(cache->tf_shpool, c->tf_node);
+        cache->tf_sh->count--;
+    }
+
+    c->tf_node = NULL;
+}
+
+
 static void
 ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
@@ -1283,6 +1334,10 @@ ngx_http_file_cache_reopen(ngx_http_requ
     c->node->count--;
     c->node = NULL;
 
+    if (c->tf_node != NULL) {
+        ngx_http_file_cache_tf_delete(c, cache);
+    }
+
     ngx_shmtx_unlock(&cache->shpool->mutex);
 
     c->secondary = 1;
@@ -1391,6 +1446,11 @@ ngx_http_file_cache_update_variant(ngx_h
     c->node->updating = 0;
     c->node = NULL;
 
+    if (c->tf_node != NULL) {
+        c->tf_node->done = 1;
+        ngx_http_file_cache_tf_delete(c, cache);
+    }
+
     ngx_shmtx_unlock(&cache->shpool->mutex);
 
     c->file.name.len = 0;
@@ -1479,6 +1539,13 @@ ngx_http_file_cache_update(ngx_http_requ
         c->node->exists = 1;
     }
 
+    if (c->tf_node != NULL) {
+        c->tf_node->updated = 1;
+        c->tf_node->done = 1;
+
+        ngx_http_file_cache_tf_delete(c, cache);
+    }
+
     c->node->updating = 0;
 
     ngx_shmtx_unlock(&cache->shpool->mutex);
@@ -1694,6 +1761,13 @@ ngx_http_file_cache_free(ngx_http_cache_
         fcn->updating = 0;
     }
 
+    if (c->tf_node != NULL) {
+        if (c->updating) {
+            c->tf_node->done = 1;
+        }
+        ngx_http_file_cache_tf_delete(c, cache);
+    }
+
     if (c->error) {
         fcn->error = c->error;
 


More information about the nginx-devel mailing list