[nginx] Upstream: simplified some code that accesses peers.

Ruslan Ermilov ru at nginx.com
Thu Jun 12 19:15:16 UTC 2014


details:   http://hg.nginx.org/nginx/rev/ab540dd44528
branches:  
changeset: 5720:ab540dd44528
user:      Ruslan Ermilov <ru at nginx.com>
date:      Thu Jun 12 21:13:24 2014 +0400
description:
Upstream: simplified some code that accesses peers.

No functional changes.

diffstat:

 src/http/ngx_http_upstream_round_robin.c |  116 ++++++++++++++++--------------
 1 files changed, 62 insertions(+), 54 deletions(-)

diffs (208 lines):

diff -r 7f425d67f91a -r ab540dd44528 src/http/ngx_http_upstream_round_robin.c
--- a/src/http/ngx_http_upstream_round_robin.c	Tue Jun 03 10:53:48 2014 -0700
+++ b/src/http/ngx_http_upstream_round_robin.c	Thu Jun 12 21:13:24 2014 +0400
@@ -30,6 +30,7 @@ ngx_http_upstream_init_round_robin(ngx_c
     ngx_url_t                      u;
     ngx_uint_t                     i, j, n, w;
     ngx_http_upstream_server_t    *server;
+    ngx_http_upstream_rr_peer_t   *peer;
     ngx_http_upstream_rr_peers_t  *peers, *backup;
 
     us->peer.init = ngx_http_upstream_init_round_robin_peer;
@@ -69,6 +70,7 @@ ngx_http_upstream_init_round_robin(ngx_c
         peers->name = &us->host;
 
         n = 0;
+        peer = peers->peer;
 
         for (i = 0; i < us->servers->nelts; i++) {
             if (server[i].backup) {
@@ -76,16 +78,16 @@ ngx_http_upstream_init_round_robin(ngx_c
             }
 
             for (j = 0; j < server[i].naddrs; j++) {
-                peers->peer[n].sockaddr = server[i].addrs[j].sockaddr;
-                peers->peer[n].socklen = server[i].addrs[j].socklen;
-                peers->peer[n].name = server[i].addrs[j].name;
-                peers->peer[n].weight = server[i].weight;
-                peers->peer[n].effective_weight = server[i].weight;
-                peers->peer[n].current_weight = 0;
-                peers->peer[n].max_fails = server[i].max_fails;
-                peers->peer[n].fail_timeout = server[i].fail_timeout;
-                peers->peer[n].down = server[i].down;
-                peers->peer[n].server = server[i].name;
+                peer[n].sockaddr = server[i].addrs[j].sockaddr;
+                peer[n].socklen = server[i].addrs[j].socklen;
+                peer[n].name = server[i].addrs[j].name;
+                peer[n].weight = server[i].weight;
+                peer[n].effective_weight = server[i].weight;
+                peer[n].current_weight = 0;
+                peer[n].max_fails = server[i].max_fails;
+                peer[n].fail_timeout = server[i].fail_timeout;
+                peer[n].down = server[i].down;
+                peer[n].server = server[i].name;
                 n++;
             }
         }
@@ -124,6 +126,7 @@ ngx_http_upstream_init_round_robin(ngx_c
         backup->name = &us->host;
 
         n = 0;
+        peer = backup->peer;
 
         for (i = 0; i < us->servers->nelts; i++) {
             if (!server[i].backup) {
@@ -131,16 +134,16 @@ ngx_http_upstream_init_round_robin(ngx_c
             }
 
             for (j = 0; j < server[i].naddrs; j++) {
-                backup->peer[n].sockaddr = server[i].addrs[j].sockaddr;
-                backup->peer[n].socklen = server[i].addrs[j].socklen;
-                backup->peer[n].name = server[i].addrs[j].name;
-                backup->peer[n].weight = server[i].weight;
-                backup->peer[n].effective_weight = server[i].weight;
-                backup->peer[n].current_weight = 0;
-                backup->peer[n].max_fails = server[i].max_fails;
-                backup->peer[n].fail_timeout = server[i].fail_timeout;
-                backup->peer[n].down = server[i].down;
-                backup->peer[n].server = server[i].name;
+                peer[n].sockaddr = server[i].addrs[j].sockaddr;
+                peer[n].socklen = server[i].addrs[j].socklen;
+                peer[n].name = server[i].addrs[j].name;
+                peer[n].weight = server[i].weight;
+                peer[n].effective_weight = server[i].weight;
+                peer[n].current_weight = 0;
+                peer[n].max_fails = server[i].max_fails;
+                peer[n].fail_timeout = server[i].fail_timeout;
+                peer[n].down = server[i].down;
+                peer[n].server = server[i].name;
                 n++;
             }
         }
@@ -189,15 +192,17 @@ ngx_http_upstream_init_round_robin(ngx_c
     peers->total_weight = n;
     peers->name = &us->host;
 
+    peer = peers->peer;
+
     for (i = 0; i < u.naddrs; i++) {
-        peers->peer[i].sockaddr = u.addrs[i].sockaddr;
-        peers->peer[i].socklen = u.addrs[i].socklen;
-        peers->peer[i].name = u.addrs[i].name;
-        peers->peer[i].weight = 1;
-        peers->peer[i].effective_weight = 1;
-        peers->peer[i].current_weight = 0;
-        peers->peer[i].max_fails = 1;
-        peers->peer[i].fail_timeout = 10;
+        peer[i].sockaddr = u.addrs[i].sockaddr;
+        peer[i].socklen = u.addrs[i].socklen;
+        peer[i].name = u.addrs[i].name;
+        peer[i].weight = 1;
+        peer[i].effective_weight = 1;
+        peer[i].current_weight = 0;
+        peer[i].max_fails = 1;
+        peer[i].fail_timeout = 10;
     }
 
     us->peer.data = peers;
@@ -271,6 +276,7 @@ ngx_http_upstream_create_round_robin_pee
     socklen_t                          socklen;
     ngx_uint_t                         i, n;
     struct sockaddr                   *sockaddr;
+    ngx_http_upstream_rr_peer_t       *peer;
     ngx_http_upstream_rr_peers_t      *peers;
     ngx_http_upstream_rr_peer_data_t  *rrp;
 
@@ -295,15 +301,17 @@ ngx_http_upstream_create_round_robin_pee
     peers->number = ur->naddrs;
     peers->name = &ur->host;
 
+    peer = peers->peer;
+
     if (ur->sockaddr) {
-        peers->peer[0].sockaddr = ur->sockaddr;
-        peers->peer[0].socklen = ur->socklen;
-        peers->peer[0].name = ur->host;
-        peers->peer[0].weight = 1;
-        peers->peer[0].effective_weight = 1;
-        peers->peer[0].current_weight = 0;
-        peers->peer[0].max_fails = 1;
-        peers->peer[0].fail_timeout = 10;
+        peer[0].sockaddr = ur->sockaddr;
+        peer[0].socklen = ur->socklen;
+        peer[0].name = ur->host;
+        peer[0].weight = 1;
+        peer[0].effective_weight = 1;
+        peer[0].current_weight = 0;
+        peer[0].max_fails = 1;
+        peer[0].fail_timeout = 10;
 
     } else {
 
@@ -335,15 +343,15 @@ ngx_http_upstream_create_round_robin_pee
 
             len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
 
-            peers->peer[i].sockaddr = sockaddr;
-            peers->peer[i].socklen = socklen;
-            peers->peer[i].name.len = len;
-            peers->peer[i].name.data = p;
-            peers->peer[i].weight = 1;
-            peers->peer[i].effective_weight = 1;
-            peers->peer[i].current_weight = 0;
-            peers->peer[i].max_fails = 1;
-            peers->peer[i].fail_timeout = 10;
+            peer[i].sockaddr = sockaddr;
+            peer[i].socklen = socklen;
+            peer[i].name.len = len;
+            peer[i].name.data = p;
+            peer[i].weight = 1;
+            peer[i].effective_weight = 1;
+            peer[i].current_weight = 0;
+            peer[i].max_fails = 1;
+            peer[i].fail_timeout = 10;
         }
     }
 
@@ -389,13 +397,15 @@ ngx_http_upstream_get_round_robin_peer(n
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
                    "get rr peer, try: %ui", pc->tries);
 
-    /* ngx_lock_mutex(rrp->peers->mutex); */
-
     pc->cached = 0;
     pc->connection = NULL;
 
-    if (rrp->peers->single) {
-        peer = &rrp->peers->peer[0];
+    peers = rrp->peers;
+
+    /* ngx_lock_mutex(peers->mutex); */
+
+    if (peers->single) {
+        peer = &peers->peer[0];
 
         if (peer->down) {
             goto failed;
@@ -420,18 +430,16 @@ ngx_http_upstream_get_round_robin_peer(n
     pc->socklen = peer->socklen;
     pc->name = &peer->name;
 
-    /* ngx_unlock_mutex(rrp->peers->mutex); */
+    /* ngx_unlock_mutex(peers->mutex); */
 
-    if (pc->tries == 1 && rrp->peers->next) {
-        pc->tries += rrp->peers->next->number;
+    if (pc->tries == 1 && peers->next) {
+        pc->tries += peers->next->number;
     }
 
     return NGX_OK;
 
 failed:
 
-    peers = rrp->peers;
-
     if (peers->next) {
 
         /* ngx_unlock_mutex(peers->mutex); */



More information about the nginx-devel mailing list