changeset 1013:7dd987e09701

stop rbtree search early if equal hash was found
author Igor Sysoev <igor@sysoev.ru>
date Thu, 11 Jan 2007 17:05:18 +0000
parents 11ffb8e4753f
children 5ffd76a9ccf3
files src/event/ngx_event_openssl.c
diffstat 1 files changed, 57 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -1413,43 +1413,52 @@ ngx_ssl_get_cached_session(ngx_ssl_conn_
             continue;
         }
 
-        if (hash == node->key && (u_char) len == node->data) {
-            sess_id = (ngx_ssl_sess_id_t *) node;
+        /* hash == node->key */
+
+        do {
+            if ((u_char) len == node->data) {
+                sess_id = (ngx_ssl_sess_id_t *) node;
+
+                if (ngx_strncmp(id, sess_id->id, len) == 0) {
 
-            if (ngx_strncmp(id, sess_id->id, len) == 0) {
+                    cached_sess = sess_id->session;
+
+                    tp = ngx_timeofday();
 
-                cached_sess = sess_id->session;
+                    if (cached_sess->expire > tp->sec) {
+                        ngx_memcpy(buf, &cached_sess->asn1[0], sess_id->len);
 
-                tp = ngx_timeofday();
+                        ngx_shmtx_unlock(&shpool->mutex);
 
-                if (cached_sess->expire > tp->sec) {
-                    ngx_memcpy(buf, &cached_sess->asn1[0], sess_id->len);
+                        p = buf;
+                        sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
 
-                    ngx_shmtx_unlock(&shpool->mutex);
+                        return sess;
+                    }
+
+                    cached_sess->next->prev = cached_sess->prev;
+                    cached_sess->prev->next = cached_sess->next;
 
-                    p = buf;
-                    sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
+                    ngx_rbtree_delete(cache->session_rbtree, node);
 
-                    return sess;
+                    ngx_slab_free_locked(shpool, cached_sess);
+                    ngx_slab_free_locked(shpool, sess_id->id);
+                    ngx_slab_free_locked(shpool, sess_id);
+
+                    sess = NULL;
+
+                    goto done;
                 }
+            }
 
-                cached_sess->next->prev = cached_sess->prev;
-                cached_sess->prev->next = cached_sess->next;
-
-                ngx_rbtree_delete(cache->session_rbtree, node);
+            node = node->right;
 
-                ngx_slab_free_locked(shpool, cached_sess);
-                ngx_slab_free_locked(shpool, sess_id->id);
-                ngx_slab_free_locked(shpool, sess_id);
-
-                sess = NULL;
+        } while (node != sentinel && hash == node->key);
 
-                break;
-            }
-        }
+        break;
+    }
 
-        node = node->right;
-    }
+done:
 
     ngx_shmtx_unlock(&shpool->mutex);
 
@@ -1500,29 +1509,38 @@ ngx_ssl_remove_session(SSL_CTX *ssl, ngx
             continue;
         }
 
-        if (hash == node->key && len == node->data) {
-            sess_id = (ngx_ssl_sess_id_t *) node;
+        /* hash == node->key */
 
-            if (ngx_strncmp(id, sess_id->id, (size_t) len) == 0) {
+        do {
+            if ((u_char) len == node->data) {
+		sess_id = (ngx_ssl_sess_id_t *) node;
 
-                cached_sess = sess_id->session;
+		if (ngx_strncmp(id, sess_id->id, (size_t) len) == 0) {
+
+		    cached_sess = sess_id->session;
 
-                cached_sess->next->prev = cached_sess->prev;
-                cached_sess->prev->next = cached_sess->next;
+		    cached_sess->next->prev = cached_sess->prev;
+		    cached_sess->prev->next = cached_sess->next;
+
+		    ngx_rbtree_delete(cache->session_rbtree, node);
 
-                ngx_rbtree_delete(cache->session_rbtree, node);
+		    ngx_slab_free_locked(shpool, cached_sess);
+		    ngx_slab_free_locked(shpool, sess_id->id);
+		    ngx_slab_free_locked(shpool, sess_id);
 
-                ngx_slab_free_locked(shpool, cached_sess);
-                ngx_slab_free_locked(shpool, sess_id->id);
-                ngx_slab_free_locked(shpool, sess_id);
+		    goto done;
+		}
+	    }
 
-                break;
-            }
-        }
+	    node = node->right;
+
+        } while (node != sentinel && hash == node->key);
 
-        node = node->right;
+        break;
     }
 
+done:
+
     ngx_shmtx_unlock(&shpool->mutex);
 }