diff src/http/modules/ngx_http_upstream_hash_module.c @ 6102:3264b7828f72

Upstreams: locking.
author Ruslan Ermilov <ru@nginx.com>
date Tue, 14 Apr 2015 19:01:23 +0300
parents c44459611d91
children b6047abf5f30
line wrap: on
line diff
--- a/src/http/modules/ngx_http_upstream_hash_module.c
+++ b/src/http/modules/ngx_http_upstream_hash_module.c
@@ -176,7 +176,10 @@ ngx_http_upstream_get_hash_peer(ngx_peer
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
                    "get hash peer, try: %ui", pc->tries);
 
+    ngx_http_upstream_rr_peers_wlock(hp->rrp.peers);
+
     if (hp->tries > 20 || hp->rrp.peers->single) {
+        ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
         return hp->get_rr_peer(pc, &hp->rrp);
     }
 
@@ -258,6 +261,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer
     next:
 
         if (++hp->tries > 20) {
+            ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
             return hp->get_rr_peer(pc, &hp->rrp);
         }
     }
@@ -274,6 +278,8 @@ ngx_http_upstream_get_hash_peer(ngx_peer
         peer->checked = now;
     }
 
+    ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
+
     hp->rrp.tried[n] |= m;
 
     return NGX_OK;
@@ -465,8 +471,13 @@ ngx_http_upstream_init_chash_peer(ngx_ht
     hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
 
     hash = ngx_crc32_long(hp->key.data, hp->key.len);
+
+    ngx_http_upstream_rr_peers_rlock(hp->rrp.peers);
+
     hp->hash = ngx_http_upstream_find_chash_point(hcf->points, hash);
 
+    ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
+
     return NGX_OK;
 }
 
@@ -489,6 +500,8 @@ ngx_http_upstream_get_chash_peer(ngx_pee
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
                    "get consistent hash peer, try: %ui", pc->tries);
 
+    ngx_http_upstream_rr_peers_wlock(hp->rrp.peers);
+
     pc->cached = 0;
     pc->connection = NULL;
 
@@ -561,6 +574,7 @@ ngx_http_upstream_get_chash_peer(ngx_pee
         hp->tries++;
 
         if (hp->tries >= points->number) {
+            ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
             return NGX_BUSY;
         }
     }
@@ -579,6 +593,8 @@ found:
         best->checked = now;
     }
 
+    ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
+
     n = best_i / (8 * sizeof(uintptr_t));
     m = (uintptr_t) 1 << best_i % (8 * sizeof(uintptr_t));