[nginx] svn commit: r4271 - trunk/src/http/modules

vbart at nginx.com vbart at nginx.com
Thu Nov 10 15:51:56 UTC 2011


Author: vbart
Date: 2011-11-10 15:51:55 +0000 (Thu, 10 Nov 2011)
New Revision: 4271

Modified:
   trunk/src/http/modules/ngx_http_limit_zone_module.c
Log:
Limit zone: rbtree lookup moved to a separate function.

No functional changes.


Modified: trunk/src/http/modules/ngx_http_limit_zone_module.c
===================================================================
--- trunk/src/http/modules/ngx_http_limit_zone_module.c	2011-11-10 09:13:09 UTC (rev 4270)
+++ trunk/src/http/modules/ngx_http_limit_zone_module.c	2011-11-10 15:51:55 UTC (rev 4271)
@@ -37,6 +37,8 @@
 } ngx_http_limit_zone_conf_t;
 
 
+static ngx_rbtree_node_t *ngx_http_limit_zone_lookup(ngx_rbtree_t *rbtree,
+    ngx_http_variable_value_t *vv, uint32_t hash);
 static void ngx_http_limit_zone_cleanup(void *data);
 
 static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf);
@@ -121,9 +123,8 @@
 {
     size_t                          len, n;
     uint32_t                        hash;
-    ngx_int_t                       rc;
     ngx_slab_pool_t                *shpool;
-    ngx_rbtree_node_t              *node, *sentinel;
+    ngx_rbtree_node_t              *node;
     ngx_pool_cleanup_t             *cln;
     ngx_http_variable_value_t      *vv;
     ngx_http_limit_zone_ctx_t      *ctx;
@@ -176,71 +177,47 @@
 
     ngx_shmtx_lock(&shpool->mutex);
 
-    node = ctx->rbtree->root;
-    sentinel = ctx->rbtree->sentinel;
+    node = ngx_http_limit_zone_lookup(ctx->rbtree, vv, hash);
 
-    while (node != sentinel) {
+    if (node == NULL) {
 
-        if (hash < node->key) {
-            node = node->left;
-            continue;
-        }
+        n = offsetof(ngx_rbtree_node_t, color)
+            + offsetof(ngx_http_limit_zone_node_t, data)
+            + len;
 
-        if (hash > node->key) {
-            node = node->right;
-            continue;
+        node = ngx_slab_alloc_locked(shpool, n);
+        if (node == NULL) {
+            ngx_shmtx_unlock(&shpool->mutex);
+            return NGX_HTTP_SERVICE_UNAVAILABLE;
         }
 
-        /* hash == node->key */
+        lz = (ngx_http_limit_zone_node_t *) &node->color;
 
-        do {
-            lz = (ngx_http_limit_zone_node_t *) &node->color;
+        node->key = hash;
+        lz->len = (u_char) len;
+        lz->conn = 1;
+        ngx_memcpy(lz->data, vv->data, len);
 
-            rc = ngx_memn2cmp(vv->data, lz->data, len, (size_t) lz->len);
+        ngx_rbtree_insert(ctx->rbtree, node);
 
-            if (rc == 0) {
-                if ((ngx_uint_t) lz->conn < lzcf->conn) {
-                    lz->conn++;
-                    goto done;
-                }
+    } else {
 
-                ngx_shmtx_unlock(&shpool->mutex);
+        lz = (ngx_http_limit_zone_node_t *) &node->color;
 
-                ngx_log_error(lzcf->log_level, r->connection->log, 0,
-                              "limiting connections by zone \"%V\"",
-                              &lzcf->shm_zone->shm.name);
+        if ((ngx_uint_t) lz->conn >= lzcf->conn) {
 
-                return NGX_HTTP_SERVICE_UNAVAILABLE;
-            }
+            ngx_shmtx_unlock(&shpool->mutex);
 
-            node = (rc < 0) ? node->left : node->right;
+            ngx_log_error(lzcf->log_level, r->connection->log, 0,
+                          "limiting connections by zone \"%V\"",
+                          &lzcf->shm_zone->shm.name);
 
-        } while (node != sentinel && hash == node->key);
+            return NGX_HTTP_SERVICE_UNAVAILABLE;
+        }
 
-        break;
+        lz->conn++;
     }
 
-    n = offsetof(ngx_rbtree_node_t, color)
-        + offsetof(ngx_http_limit_zone_node_t, data)
-        + len;
-
-    node = ngx_slab_alloc_locked(shpool, n);
-    if (node == NULL) {
-        ngx_shmtx_unlock(&shpool->mutex);
-        return NGX_HTTP_SERVICE_UNAVAILABLE;
-    }
-
-    lz = (ngx_http_limit_zone_node_t *) &node->color;
-
-    node->key = hash;
-    lz->len = (u_char) len;
-    lz->conn = 1;
-    ngx_memcpy(lz->data, vv->data, len);
-
-    ngx_rbtree_insert(ctx->rbtree, node);
-
-done:
-
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "limit zone: %08XD %d", node->key, lz->conn);
 
@@ -297,6 +274,51 @@
 }
 
 
+static ngx_rbtree_node_t *
+ngx_http_limit_zone_lookup(ngx_rbtree_t *rbtree, ngx_http_variable_value_t *vv,
+    uint32_t hash)
+{
+    ngx_int_t                    rc;
+    ngx_rbtree_node_t           *node, *sentinel;
+    ngx_http_limit_zone_node_t  *lzn;
+
+    node = rbtree->root;
+    sentinel = rbtree->sentinel;
+
+    while (node != sentinel) {
+
+        if (hash < node->key) {
+            node = node->left;
+            continue;
+        }
+
+        if (hash > node->key) {
+            node = node->right;
+            continue;
+        }
+
+        /* hash == node->key */
+
+        do {
+            lzn = (ngx_http_limit_zone_node_t *) &node->color;
+
+            rc = ngx_memn2cmp(vv->data, lzn->data,
+                              (size_t) vv->len, (size_t) lzn->len);
+            if (rc == 0) {
+                return node;
+            }
+
+            node = (rc < 0) ? node->left : node->right;
+
+        } while (node != sentinel && hash == node->key);
+
+        break;
+    }
+
+    return NULL;
+}
+
+
 static void
 ngx_http_limit_zone_cleanup(void *data)
 {



More information about the nginx-devel mailing list