[PATCH] The "sort=" parameter of the "resolver" directive
Ruslan Ermilov
ru at nginx.com
Wed Feb 23 04:11:25 UTC 2022
src/core/ngx_resolver.c | 38 +++++++++++++++++++++++++++++++++++++-
src/core/ngx_resolver.h | 5 +++++
2 files changed, 42 insertions(+), 1 deletions(-)
# HG changeset patch
# User Ruslan Ermilov <ru at nginx.com>
# Date 1645589387 -10800
# Wed Feb 23 07:09:47 2022 +0300
# Node ID 8db4bbd67840e8bebb23f9c6d10c0f633552e616
# Parent 1c19779448db2309d607c74e2628ff98f84569ff
The "sort=" parameter of the "resolver" directive.
diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -266,6 +266,27 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_
}
#endif
+ if (ngx_strncmp(names[i].data, "sort=", 5) == 0) {
+
+ if (ngx_strcasecmp(&names[i].data[5], (u_char *) "ipv4") == 0) {
+ r->sort = NGX_RESOLVE_A_FIRST;
+
+#if (NGX_HAVE_INET6)
+ } else if (ngx_strcasecmp(&names[i].data[5], (u_char *) "ipv6")
+ == 0)
+ {
+ r->sort = NGX_RESOLVE_AAAA_FIRST;
+#endif
+
+ } else {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid parameter: %V", &names[i]);
+ return NULL;
+ }
+
+ continue;
+ }
+
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = names[i];
@@ -4253,7 +4274,22 @@ ngx_resolver_export(ngx_resolver_t *r, n
}
i = 0;
- d = rotate ? ngx_random() % n : 0;
+
+ if (r->sort == NGX_RESOLVE_A_FIRST) {
+ d = 0;
+
+#if (NGX_HAVE_INET6)
+ } else if (r->sort == NGX_RESOLVE_AAAA_FIRST) {
+ d = rn->naddrs6;
+
+ if (d == n) {
+ d = 0;
+ }
+#endif
+
+ } else {
+ d = rotate ? ngx_random() % n : 0;
+ }
if (rn->naddrs) {
j = rotate ? ngx_random() % rn->naddrs : 0;
diff --git a/src/core/ngx_resolver.h b/src/core/ngx_resolver.h
--- a/src/core/ngx_resolver.h
+++ b/src/core/ngx_resolver.h
@@ -36,6 +36,9 @@
#define NGX_RESOLVER_MAX_RECURSION 50
+#define NGX_RESOLVE_A_FIRST 1
+#define NGX_RESOLVE_AAAA_FIRST 2
+
typedef struct ngx_resolver_s ngx_resolver_t;
@@ -185,6 +188,8 @@ struct ngx_resolver_s {
ngx_queue_t addr6_expire_queue;
#endif
+ ngx_uint_t sort;
+
time_t resend_timeout;
time_t tcp_timeout;
time_t expire;
More information about the nginx-devel
mailing list