# HG changeset patch # User Vladimir Homutov # Date 1386582207 -14400 # Node ID 741aa3fde4963e13430bad3d59ac4177b79d0ba4 # Parent 8958656a8060ff395b3f1a417266ebf261136625 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. diff --git a/src/http/modules/ngx_http_upstream_ip_hash_module.c b/src/http/modules/ngx_http_upstream_ip_hash_module.c --- a/src/http/modules/ngx_http_upstream_ip_hash_module.c +++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c @@ -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; + } + + break; - if (now - peer->checked > peer->fail_timeout) { - peer->checked = now; - break; - } - } + next_try: + + iphp->rrp.tried[n] |= m; - iphp->rrp.tried[n] |= m; + /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */ - /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */ + pc->tries--; - 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;