[nginx] Upstream: fixed shared upstreams on win32.

Ruslan Ermilov ru at nginx.com
Thu Jun 18 14:34:09 UTC 2015


details:   http://hg.nginx.org/nginx/rev/311d232ad803
branches:  
changeset: 6198:311d232ad803
user:      Ruslan Ermilov <ru at nginx.com>
date:      Tue Jun 16 00:43:00 2015 +0300
description:
Upstream: fixed shared upstreams on win32.

diffstat:

 src/http/modules/ngx_http_upstream_zone_module.c |  43 +++++++++++++++++------
 src/http/ngx_http_upstream_round_robin.h         |   1 +
 src/stream/ngx_stream_upstream_round_robin.h     |   1 +
 src/stream/ngx_stream_upstream_zone_module.c     |  43 +++++++++++++++++------
 4 files changed, 64 insertions(+), 24 deletions(-)

diffs (264 lines):

diff -r 0dcef374b8bb -r 311d232ad803 src/http/modules/ngx_http_upstream_zone_module.c
--- a/src/http/modules/ngx_http_upstream_zone_module.c	Thu Jun 18 14:17:30 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_zone_module.c	Tue Jun 16 00:43:00 2015 +0300
@@ -14,8 +14,8 @@ static char *ngx_http_upstream_zone(ngx_
     void *conf);
 static ngx_int_t ngx_http_upstream_init_zone(ngx_shm_zone_t *shm_zone,
     void *data);
-static ngx_int_t ngx_http_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
-    ngx_http_upstream_srv_conf_t *uscf);
+static ngx_http_upstream_rr_peers_t *ngx_http_upstream_zone_copy_peers(
+    ngx_slab_pool_t *shpool, ngx_http_upstream_srv_conf_t *uscf);
 
 
 static ngx_command_t  ngx_http_upstream_zone_commands[] = {
@@ -121,13 +121,29 @@ ngx_http_upstream_init_zone(ngx_shm_zone
     size_t                          len;
     ngx_uint_t                      i;
     ngx_slab_pool_t                *shpool;
+    ngx_http_upstream_rr_peers_t   *peers, **peersp;
     ngx_http_upstream_srv_conf_t   *uscf, **uscfp;
     ngx_http_upstream_main_conf_t  *umcf;
 
     shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
+    umcf = shm_zone->data;
+    uscfp = umcf->upstreams.elts;
 
     if (shm_zone->shm.exists) {
-        return NGX_ERROR;
+        peers = shpool->data;
+
+        for (i = 0; i < umcf->upstreams.nelts; i++) {
+            uscf = uscfp[i];
+
+            if (uscf->shm_zone != shm_zone) {
+                continue;
+            }
+
+            uscf->peer.data = peers;
+            peers = peers->zone_next;
+        }
+
+        return NGX_OK;
     }
 
     len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len;
@@ -143,8 +159,7 @@ ngx_http_upstream_init_zone(ngx_shm_zone
 
     /* copy peers to shared memory */
 
-    umcf = shm_zone->data;
-    uscfp = umcf->upstreams.elts;
+    peersp = (ngx_http_upstream_rr_peers_t **) &shpool->data;
 
     for (i = 0; i < umcf->upstreams.nelts; i++) {
         uscf = uscfp[i];
@@ -153,16 +168,20 @@ ngx_http_upstream_init_zone(ngx_shm_zone
             continue;
         }
 
-        if (ngx_http_upstream_zone_copy_peers(shpool, uscf) != NGX_OK) {
+        peers = ngx_http_upstream_zone_copy_peers(shpool, uscf);
+        if (peers == NULL) {
             return NGX_ERROR;
         }
+
+        *peersp = peers;
+        peersp = &peers->zone_next;
     }
 
     return NGX_OK;
 }
 
 
-static ngx_int_t
+static ngx_http_upstream_rr_peers_t *
 ngx_http_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
     ngx_http_upstream_srv_conf_t *uscf)
 {
@@ -171,7 +190,7 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
 
     peers = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t));
     if (peers == NULL) {
-        return NGX_ERROR;
+        return NULL;
     }
 
     ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_http_upstream_rr_peers_t));
@@ -183,7 +202,7 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
         peer = ngx_slab_calloc_locked(shpool,
                                       sizeof(ngx_http_upstream_rr_peer_t));
         if (peer == NULL) {
-            return NGX_ERROR;
+            return NULL;
         }
 
         ngx_memcpy(peer, *peerp, sizeof(ngx_http_upstream_rr_peer_t));
@@ -197,7 +216,7 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
 
     backup = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t));
     if (backup == NULL) {
-        return NGX_ERROR;
+        return NULL;
     }
 
     ngx_memcpy(backup, peers->next, sizeof(ngx_http_upstream_rr_peers_t));
@@ -209,7 +228,7 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
         peer = ngx_slab_calloc_locked(shpool,
                                       sizeof(ngx_http_upstream_rr_peer_t));
         if (peer == NULL) {
-            return NGX_ERROR;
+            return NULL;
         }
 
         ngx_memcpy(peer, *peerp, sizeof(ngx_http_upstream_rr_peer_t));
@@ -223,5 +242,5 @@ done:
 
     uscf->peer.data = peers;
 
-    return NGX_OK;
+    return peers;
 }
diff -r 0dcef374b8bb -r 311d232ad803 src/http/ngx_http_upstream_round_robin.h
--- a/src/http/ngx_http_upstream_round_robin.h	Thu Jun 18 14:17:30 2015 +0300
+++ b/src/http/ngx_http_upstream_round_robin.h	Tue Jun 16 00:43:00 2015 +0300
@@ -58,6 +58,7 @@ struct ngx_http_upstream_rr_peers_s {
 #if (NGX_HTTP_UPSTREAM_ZONE)
     ngx_slab_pool_t                *shpool;
     ngx_atomic_t                    rwlock;
+    ngx_http_upstream_rr_peers_t   *zone_next;
 #endif
 
     ngx_uint_t                      total_weight;
diff -r 0dcef374b8bb -r 311d232ad803 src/stream/ngx_stream_upstream_round_robin.h
--- a/src/stream/ngx_stream_upstream_round_robin.h	Thu Jun 18 14:17:30 2015 +0300
+++ b/src/stream/ngx_stream_upstream_round_robin.h	Tue Jun 16 00:43:00 2015 +0300
@@ -58,6 +58,7 @@ struct ngx_stream_upstream_rr_peers_s {
 #if (NGX_STREAM_UPSTREAM_ZONE)
     ngx_slab_pool_t                 *shpool;
     ngx_atomic_t                     rwlock;
+    ngx_stream_upstream_rr_peers_t  *zone_next;
 #endif
 
     ngx_uint_t                       total_weight;
diff -r 0dcef374b8bb -r 311d232ad803 src/stream/ngx_stream_upstream_zone_module.c
--- a/src/stream/ngx_stream_upstream_zone_module.c	Thu Jun 18 14:17:30 2015 +0300
+++ b/src/stream/ngx_stream_upstream_zone_module.c	Tue Jun 16 00:43:00 2015 +0300
@@ -14,8 +14,8 @@ static char *ngx_stream_upstream_zone(ng
     void *conf);
 static ngx_int_t ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone,
     void *data);
-static ngx_int_t ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
-    ngx_stream_upstream_srv_conf_t *uscf);
+static ngx_stream_upstream_rr_peers_t *ngx_stream_upstream_zone_copy_peers(
+    ngx_slab_pool_t *shpool, ngx_stream_upstream_srv_conf_t *uscf);
 
 
 static ngx_command_t  ngx_stream_upstream_zone_commands[] = {
@@ -117,13 +117,29 @@ ngx_stream_upstream_init_zone(ngx_shm_zo
     size_t                            len;
     ngx_uint_t                        i;
     ngx_slab_pool_t                  *shpool;
+    ngx_stream_upstream_rr_peers_t   *peers, **peersp;
     ngx_stream_upstream_srv_conf_t   *uscf, **uscfp;
     ngx_stream_upstream_main_conf_t  *umcf;
 
     shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
+    umcf = shm_zone->data;
+    uscfp = umcf->upstreams.elts;
 
     if (shm_zone->shm.exists) {
-        return NGX_ERROR;
+        peers = shpool->data;
+
+        for (i = 0; i < umcf->upstreams.nelts; i++) {
+            uscf = uscfp[i];
+
+            if (uscf->shm_zone != shm_zone) {
+                continue;
+            }
+
+            uscf->peer.data = peers;
+            peers = peers->zone_next;
+        }
+
+        return NGX_OK;
     }
 
     len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len;
@@ -139,8 +155,7 @@ ngx_stream_upstream_init_zone(ngx_shm_zo
 
     /* copy peers to shared memory */
 
-    umcf = shm_zone->data;
-    uscfp = umcf->upstreams.elts;
+    peersp = (ngx_stream_upstream_rr_peers_t **) &shpool->data;
 
     for (i = 0; i < umcf->upstreams.nelts; i++) {
         uscf = uscfp[i];
@@ -149,16 +164,20 @@ ngx_stream_upstream_init_zone(ngx_shm_zo
             continue;
         }
 
-        if (ngx_stream_upstream_zone_copy_peers(shpool, uscf) != NGX_OK) {
+        peers = ngx_stream_upstream_zone_copy_peers(shpool, uscf);
+        if (peers == NULL) {
             return NGX_ERROR;
         }
+
+        *peersp = peers;
+        peersp = &peers->zone_next;
     }
 
     return NGX_OK;
 }
 
 
-static ngx_int_t
+static ngx_stream_upstream_rr_peers_t *
 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
     ngx_stream_upstream_srv_conf_t *uscf)
 {
@@ -167,7 +186,7 @@ ngx_stream_upstream_zone_copy_peers(ngx_
 
     peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t));
     if (peers == NULL) {
-        return NGX_ERROR;
+        return NULL;
     }
 
     ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t));
@@ -179,7 +198,7 @@ ngx_stream_upstream_zone_copy_peers(ngx_
         peer = ngx_slab_calloc_locked(shpool,
                                       sizeof(ngx_stream_upstream_rr_peer_t));
         if (peer == NULL) {
-            return NGX_ERROR;
+            return NULL;
         }
 
         ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t));
@@ -193,7 +212,7 @@ ngx_stream_upstream_zone_copy_peers(ngx_
 
     backup = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t));
     if (backup == NULL) {
-        return NGX_ERROR;
+        return NULL;
     }
 
     ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t));
@@ -205,7 +224,7 @@ ngx_stream_upstream_zone_copy_peers(ngx_
         peer = ngx_slab_calloc_locked(shpool,
                                       sizeof(ngx_stream_upstream_rr_peer_t));
         if (peer == NULL) {
-            return NGX_ERROR;
+            return NULL;
         }
 
         ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t));
@@ -219,5 +238,5 @@ done:
 
     uscf->peer.data = peers;
 
-    return NGX_OK;
+    return peers;
 }



More information about the nginx-devel mailing list