[nginx] Upstream: simplified ip_hash and hash peer selection code.

Ruslan Ermilov ru at nginx.com
Tue Apr 21 20:04:14 UTC 2015


details:   http://hg.nginx.org/nginx/rev/b6047abf5f30
branches:  
changeset: 6121:b6047abf5f30
user:      Ruslan Ermilov <ru at nginx.com>
date:      Tue Apr 21 19:09:04 2015 +0300
description:
Upstream: simplified ip_hash and hash peer selection code.

Now that peers are stored as a list, the weighted and unweighted
cases became nearly identical.

diffstat:

 src/http/modules/ngx_http_upstream_hash_module.c    |  30 +++++---------------
 src/http/modules/ngx_http_upstream_ip_hash_module.c |  28 ++++--------------
 src/stream/ngx_stream_upstream_hash_module.c        |  30 +++++---------------
 3 files changed, 23 insertions(+), 65 deletions(-)

diffs (135 lines):

diff -r 1bdfceda86a9 -r b6047abf5f30 src/http/modules/ngx_http_upstream_hash_module.c
--- a/src/http/modules/ngx_http_upstream_hash_module.c	Mon Apr 20 17:36:51 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_hash_module.c	Tue Apr 21 19:09:04 2015 +0300
@@ -170,7 +170,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer
     uint32_t                      hash;
     ngx_int_t                     w;
     uintptr_t                     m;
-    ngx_uint_t                    i, n, p;
+    ngx_uint_t                    n, p;
     ngx_http_upstream_rr_peer_t  *peer;
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
@@ -211,28 +211,14 @@ ngx_http_upstream_get_hash_peer(ngx_peer
         hp->hash += hash;
         hp->rehash++;
 
-        if (!hp->rrp.peers->weighted) {
-            p = hp->hash % hp->rrp.peers->number;
+        w = hp->hash % hp->rrp.peers->total_weight;
+        peer = hp->rrp.peers->peer;
+        p = 0;
 
-            peer = hp->rrp.peers->peer;
-            for (i = 0; i < p; i++) {
-                peer = peer->next;
-            }
-
-        } else {
-            w = hp->hash % hp->rrp.peers->total_weight;
-
-            for (peer = hp->rrp.peers->peer, i = 0;
-                 peer;
-                 peer = peer->next, i++)
-            {
-                w -= peer->weight;
-                if (w < 0) {
-                    break;
-                }
-            }
-
-            p = i;
+        while (w >= peer->weight) {
+            w -= peer->weight;
+            peer = peer->next;
+            p++;
         }
 
         n = p / (8 * sizeof(uintptr_t));
diff -r 1bdfceda86a9 -r b6047abf5f30 src/http/modules/ngx_http_upstream_ip_hash_module.c
--- a/src/http/modules/ngx_http_upstream_ip_hash_module.c	Mon Apr 20 17:36:51 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c	Tue Apr 21 19:09:04 2015 +0300
@@ -181,28 +181,14 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
             hash = (hash * 113 + iphp->addr[i]) % 6271;
         }
 
-        if (!iphp->rrp.peers->weighted) {
-            p = hash % iphp->rrp.peers->number;
+        w = hash % iphp->rrp.peers->total_weight;
+        peer = iphp->rrp.peers->peer;
+        p = 0;
 
-            peer = iphp->rrp.peers->peer;
-            for (i = 0; i < p; i++) {
-                peer = peer->next;
-            }
-
-        } else {
-            w = hash % iphp->rrp.peers->total_weight;
-
-            for (peer = iphp->rrp.peers->peer, i = 0;
-                 peer;
-                 peer = peer->next, i++)
-            {
-                w -= peer->weight;
-                if (w < 0) {
-                    break;
-                }
-            }
-
-            p = i;
+        while (w >= peer->weight) {
+            w -= peer->weight;
+            peer = peer->next;
+            p++;
         }
 
         n = p / (8 * sizeof(uintptr_t));
diff -r 1bdfceda86a9 -r b6047abf5f30 src/stream/ngx_stream_upstream_hash_module.c
--- a/src/stream/ngx_stream_upstream_hash_module.c	Mon Apr 20 17:36:51 2015 +0300
+++ b/src/stream/ngx_stream_upstream_hash_module.c	Tue Apr 21 19:09:04 2015 +0300
@@ -164,7 +164,7 @@ ngx_stream_upstream_get_hash_peer(ngx_pe
     uint32_t                        hash;
     ngx_int_t                       w;
     uintptr_t                       m;
-    ngx_uint_t                      i, n, p;
+    ngx_uint_t                      n, p;
     ngx_stream_upstream_rr_peer_t  *peer;
 
     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
@@ -204,28 +204,14 @@ ngx_stream_upstream_get_hash_peer(ngx_pe
         hp->hash += hash;
         hp->rehash++;
 
-        if (!hp->rrp.peers->weighted) {
-            p = hp->hash % hp->rrp.peers->number;
+        w = hp->hash % hp->rrp.peers->total_weight;
+        peer = hp->rrp.peers->peer;
+        p = 0;
 
-            peer = hp->rrp.peers->peer;
-            for (i = 0; i < p; i++) {
-                peer = peer->next;
-            }
-
-        } else {
-            w = hp->hash % hp->rrp.peers->total_weight;
-
-            for (peer = hp->rrp.peers->peer, i = 0;
-                 peer;
-                 peer = peer->next, i++)
-            {
-                w -= peer->weight;
-                if (w < 0) {
-                    break;
-                }
-            }
-
-            p = i;
+        while (w >= peer->weight) {
+            w -= peer->weight;
+            peer = peer->next;
+            p++;
         }
 
         n = p / (8 * sizeof(uintptr_t));



More information about the nginx-devel mailing list