[nginx] Upstream: simplified peer selection loop in the "ip_hash...

Homutov Vladimir vl at nginx.com
Fri Dec 20 06:21:02 UTC 2013


details:   http://hg.nginx.org/nginx/rev/741aa3fde496
branches:  
changeset: 5486:741aa3fde496
user:      Vladimir Homutov <vl at nginx.com>
date:      Mon Dec 09 13:43:27 2013 +0400
description:
Upstream: simplified peer selection loop in the "ip_hash" module.

Conditions for skipping ineligible peers are rewritten to make adding of new
conditions simpler and be in line with the "round_robin" and "least_conn"
modules.  No functional changes.

diffstat:

 src/http/modules/ngx_http_upstream_ip_hash_module.c |  46 ++++++++++++--------
 1 files changed, 28 insertions(+), 18 deletions(-)

diffs (72 lines):

diff -r 8958656a8060 -r 741aa3fde496 src/http/modules/ngx_http_upstream_ip_hash_module.c
--- a/src/http/modules/ngx_http_upstream_ip_hash_module.c	Mon Dec 16 19:12:23 2013 +0400
+++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c	Mon Dec 09 13:43:27 2013 +0400
@@ -197,33 +197,39 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
         n = p / (8 * sizeof(uintptr_t));
         m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
 
-        if (!(iphp->rrp.tried[n] & m)) {
+        if (iphp->rrp.tried[n] & m) {
+            goto next;
+        }
 
-            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
-                           "get ip hash peer, hash: %ui %04XA", p, m);
+        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+                       "get ip hash peer, hash: %ui %04XA", p, m);
 
-            peer = &iphp->rrp.peers->peer[p];
+        peer = &iphp->rrp.peers->peer[p];
 
-            /* ngx_lock_mutex(iphp->rrp.peers->mutex); */
+        /* ngx_lock_mutex(iphp->rrp.peers->mutex); */
 
-            if (!peer->down) {
+        if (peer->down) {
+            goto next_try;
+        }
 
-                if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
-                    break;
-                }
+        if (peer->max_fails
+            && peer->fails >= peer->max_fails
+            && now - peer->checked <= peer->fail_timeout)
+        {
+            goto next_try;
+        }
 
-                if (now - peer->checked > peer->fail_timeout) {
-                    peer->checked = now;
-                    break;
-                }
-            }
+        break;
 
-            iphp->rrp.tried[n] |= m;
+    next_try:
 
-            /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
+        iphp->rrp.tried[n] |= m;
 
-            pc->tries--;
-        }
+        /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
+
+        pc->tries--;
+
+    next:
 
         if (++iphp->tries >= 20) {
             return iphp->get_rr_peer(pc, &iphp->rrp);
@@ -236,6 +242,10 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
     pc->socklen = peer->socklen;
     pc->name = &peer->name;
 
+    if (now - peer->checked > peer->fail_timeout) {
+        peer->checked = now;
+    }
+
     /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
 
     iphp->rrp.tried[n] |= m;



More information about the nginx-devel mailing list