[nginx] Upstream zone: store peers->name and its data in shared memory.

Ruslan Ermilov ru at nginx.com
Tue Aug 1 16:13:12 UTC 2017


details:   http://hg.nginx.org/nginx/rev/1eb753aa8e5e
branches:  
changeset: 7078:1eb753aa8e5e
user:      Ruslan Ermilov <ru at nginx.com>
date:      Tue Aug 01 19:12:10 2017 +0300
description:
Upstream zone: store peers->name and its data in shared memory.

The shared objects should generally be allocated from shared memory.
While peers->name and the data it points to allocated from cf->pool
happened to work on UNIX, it broke on Windows.  On UNIX this worked
only because the shared memory zone for upstreams is re-created for
every new configuration.

But on Windows, a worker process does not inherit the address space
of the master process, so the peers->name pointed to data allocated
from cf->pool by the master process, and was invalid.

diffstat:

 src/http/modules/ngx_http_upstream_zone_module.c |  18 ++++++++++++++++++
 src/stream/ngx_stream_upstream_zone_module.c     |  18 ++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diffs (84 lines):

diff -r 2a288909abc6 -r 1eb753aa8e5e src/http/modules/ngx_http_upstream_zone_module.c
--- a/src/http/modules/ngx_http_upstream_zone_module.c	Tue Aug 01 14:28:33 2017 +0300
+++ b/src/http/modules/ngx_http_upstream_zone_module.c	Tue Aug 01 19:12:10 2017 +0300
@@ -185,6 +185,7 @@ 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)
 {
+    ngx_str_t                     *name;
     ngx_http_upstream_rr_peer_t   *peer, **peerp;
     ngx_http_upstream_rr_peers_t  *peers, *backup;
 
@@ -195,6 +196,21 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
 
     ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_http_upstream_rr_peers_t));
 
+    name = ngx_slab_alloc(shpool, sizeof(ngx_str_t));
+    if (name == NULL) {
+        return NULL;
+    }
+
+    name->data = ngx_slab_alloc(shpool, peers->name->len);
+    if (name->data == NULL) {
+        return NULL;
+    }
+
+    ngx_memcpy(name->data, peers->name->data, peers->name->len);
+    name->len = peers->name->len;
+
+    peers->name = name;
+
     peers->shpool = shpool;
 
     for (peerp = &peers->peer; *peerp; peerp = &peer->next) {
@@ -221,6 +237,8 @@ ngx_http_upstream_zone_copy_peers(ngx_sl
 
     ngx_memcpy(backup, peers->next, sizeof(ngx_http_upstream_rr_peers_t));
 
+    backup->name = name;
+
     backup->shpool = shpool;
 
     for (peerp = &backup->peer; *peerp; peerp = &peer->next) {
diff -r 2a288909abc6 -r 1eb753aa8e5e src/stream/ngx_stream_upstream_zone_module.c
--- a/src/stream/ngx_stream_upstream_zone_module.c	Tue Aug 01 14:28:33 2017 +0300
+++ b/src/stream/ngx_stream_upstream_zone_module.c	Tue Aug 01 19:12:10 2017 +0300
@@ -182,6 +182,7 @@ 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)
 {
+    ngx_str_t                       *name;
     ngx_stream_upstream_rr_peer_t   *peer, **peerp;
     ngx_stream_upstream_rr_peers_t  *peers, *backup;
 
@@ -192,6 +193,21 @@ ngx_stream_upstream_zone_copy_peers(ngx_
 
     ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t));
 
+    name = ngx_slab_alloc(shpool, sizeof(ngx_str_t));
+    if (name == NULL) {
+        return NULL;
+    }
+
+    name->data = ngx_slab_alloc(shpool, peers->name->len);
+    if (name->data == NULL) {
+        return NULL;
+    }
+
+    ngx_memcpy(name->data, peers->name->data, peers->name->len);
+    name->len = peers->name->len;
+
+    peers->name = name;
+
     peers->shpool = shpool;
 
     for (peerp = &peers->peer; *peerp; peerp = &peer->next) {
@@ -218,6 +234,8 @@ ngx_stream_upstream_zone_copy_peers(ngx_
 
     ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t));
 
+    backup->name = name;
+
     backup->shpool = shpool;
 
     for (peerp = &backup->peer; *peerp; peerp = &peer->next) {


More information about the nginx-devel mailing list