[nginx] svn commit: r4904 - in branches/stable-1.2: . src/core

mdounin at mdounin.ru mdounin at mdounin.ru
Mon Nov 12 18:47:08 UTC 2012


Author: mdounin
Date: 2012-11-12 18:47:07 +0000 (Mon, 12 Nov 2012)
New Revision: 4904
URL: http://trac.nginx.org/nginx/changeset/4904/nginx

Log:
Merge of r4872, r4893: resolver cached addresses random rotation.

Resolver: cached addresses are returned with random rotation now.

This ensures balancing when working with dynamically resolved upstream
servers with multiple addresses.

Based on patch by Anton Jouline.


Modified:
   branches/stable-1.2/
   branches/stable-1.2/src/core/ngx_resolver.c

Index: branches/stable-1.2
===================================================================
--- branches/stable-1.2	2012-11-12 18:39:51 UTC (rev 4903)
+++ branches/stable-1.2	2012-11-12 18:47:07 UTC (rev 4904)

Property changes on: branches/stable-1.2
___________________________________________________________________
Modified: svn:mergeinfo
## -1 +1 ##
-/trunk:4611-4632,4636-4657,4671-4672,4674-4676,4682,4684-4699,4704-4706,4713,4736-4741,4754,4756-4771,4775,4777-4780,4782-4785,4795,4811-4820,4822-4824,4828-4835,4840-4844,4865-4871,4890,4895
+/trunk:4611-4632,4636-4657,4671-4672,4674-4676,4682,4684-4699,4704-4706,4713,4736-4741,4754,4756-4771,4775,4777-4780,4782-4785,4795,4811-4820,4822-4824,4828-4835,4840-4844,4865-4872,4890,4893,4895
\ No newline at end of property
Modified: branches/stable-1.2/src/core/ngx_resolver.c
===================================================================
--- branches/stable-1.2/src/core/ngx_resolver.c	2012-11-12 18:39:51 UTC (rev 4903)
+++ branches/stable-1.2/src/core/ngx_resolver.c	2012-11-12 18:47:07 UTC (rev 4904)
@@ -88,6 +88,8 @@
 static void ngx_resolver_free(ngx_resolver_t *r, void *p);
 static void ngx_resolver_free_locked(ngx_resolver_t *r, void *p);
 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size);
+static in_addr_t *ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src,
+    ngx_uint_t n);
 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
 
 
@@ -445,8 +447,7 @@
 
                 if (naddrs != 1) {
                     addr = 0;
-                    addrs = ngx_resolver_dup(r, rn->u.addrs,
-                                             naddrs * sizeof(in_addr_t));
+                    addrs = ngx_resolver_rotate(r, rn->u.addrs, naddrs);
                     if (addrs == NULL) {
                         return NGX_ERROR;
                     }
@@ -2135,6 +2136,32 @@
 }
 
 
+static in_addr_t *
+ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src, ngx_uint_t n)
+{
+    void        *dst, *p;
+    ngx_uint_t   j;
+
+    dst = ngx_resolver_alloc(r, n * sizeof(in_addr_t));
+
+    if (dst == NULL) {
+        return dst;
+    }
+
+    j = ngx_random() % n;
+
+    if (j == 0) {
+        ngx_memcpy(dst, src, n * sizeof(in_addr_t));
+        return dst;
+    }
+
+    p = ngx_cpymem(dst, &src[j], (n - j) * sizeof(in_addr_t));
+    ngx_memcpy(p, src, j * sizeof(in_addr_t));
+
+    return dst;
+}
+
+
 char *
 ngx_resolver_strerror(ngx_int_t err)
 {



More information about the nginx-devel mailing list