aboutsummaryrefslogtreecommitdiff
path: root/src/stream/ngx_stream_upstream.c
diff options
context:
space:
mode:
authorRuslan Ermilov <ru@nginx.com>2014-02-15 15:12:34 +0400
committerAleksei Bavshin <a.bavshin@f5.com>2024-11-07 07:57:42 -0800
commitdb6870e06dde7ab249e9a41a0e0a76219f82dd8c (patch)
treed24f1bb15d855a4a85f1240cc4bbbc9dbf6f74be /src/stream/ngx_stream_upstream.c
parentebd18ec1812bd6f3de54d9f9fc81563a0ec9f264 (diff)
downloadnginx-db6870e06dde7ab249e9a41a0e0a76219f82dd8c.tar.gz
nginx-db6870e06dde7ab249e9a41a0e0a76219f82dd8c.zip
Upstream: re-resolvable servers.
Specifying the upstream server by a hostname together with the "resolve" parameter will make the hostname to be periodically resolved, and upstream servers added/removed as necessary. This requires a "resolver" at the "http" configuration block. The "resolver_timeout" parameter also affects when the failed DNS requests will be attempted again. Responses with NXDOMAIN will be attempted again in 10 seconds. Upstream has a configuration generation number that is incremented each time servers are added/removed to the primary/backup list. This number is remembered by the peer.init method, and if peer.get detects a change in configuration, it returns NGX_BUSY. Each server has a reference counter. It is incremented by peer.get and decremented by peer.free. When a server is removed, it is removed from the list of servers and is marked as "zombie". The memory allocated by a zombie peer is freed only when its reference count becomes zero. Co-authored-by: Roman Arutyunyan <arut@nginx.com> Co-authored-by: Sergey Kandaurov <pluknet@nginx.com> Co-authored-by: Vladimir Homutov <vl@nginx.com>
Diffstat (limited to 'src/stream/ngx_stream_upstream.c')
-rw-r--r--src/stream/ngx_stream_upstream.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/stream/ngx_stream_upstream.c b/src/stream/ngx_stream_upstream.c
index eadcf9f9a..a251cca00 100644
--- a/src/stream/ngx_stream_upstream.c
+++ b/src/stream/ngx_stream_upstream.c
@@ -319,6 +319,7 @@ ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
u.no_port = 1;
uscf = ngx_stream_upstream_add(cf, &u, NGX_STREAM_UPSTREAM_CREATE
+ |NGX_STREAM_UPSTREAM_MODIFY
|NGX_STREAM_UPSTREAM_WEIGHT
|NGX_STREAM_UPSTREAM_MAX_CONNS
|NGX_STREAM_UPSTREAM_MAX_FAILS
@@ -408,6 +409,9 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_url_t u;
ngx_int_t weight, max_conns, max_fails;
ngx_uint_t i;
+#if (NGX_STREAM_UPSTREAM_ZONE)
+ ngx_uint_t resolve;
+#endif
ngx_stream_upstream_server_t *us;
us = ngx_array_push(uscf->servers);
@@ -423,6 +427,9 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
max_conns = 0;
max_fails = 1;
fail_timeout = 10;
+#if (NGX_STREAM_UPSTREAM_ZONE)
+ resolve = 0;
+#endif
for (i = 2; i < cf->args->nelts; i++) {
@@ -511,6 +518,13 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
continue;
}
+#if (NGX_STREAM_UPSTREAM_ZONE)
+ if (ngx_strcmp(value[i].data, "resolve") == 0) {
+ resolve = 1;
+ continue;
+ }
+#endif
+
goto invalid;
}
@@ -518,6 +532,13 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
u.url = value[1];
+#if (NGX_STREAM_UPSTREAM_ZONE)
+ if (resolve) {
+ /* resolve at run time */
+ u.no_resolve = 1;
+ }
+#endif
+
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
@@ -534,8 +555,45 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
us->name = u.url;
+
+#if (NGX_STREAM_UPSTREAM_ZONE)
+
+ if (resolve && u.naddrs == 0) {
+ ngx_addr_t *addr;
+
+ /* save port */
+
+ addr = ngx_pcalloc(cf->pool, sizeof(ngx_addr_t));
+ if (addr == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ addr->sockaddr = ngx_palloc(cf->pool, u.socklen);
+ if (addr->sockaddr == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ngx_memcpy(addr->sockaddr, &u.sockaddr, u.socklen);
+
+ addr->socklen = u.socklen;
+
+ us->addrs = addr;
+ us->naddrs = 1;
+
+ us->host = u.host;
+
+ } else {
+ us->addrs = u.addrs;
+ us->naddrs = u.naddrs;
+ }
+
+#else
+
us->addrs = u.addrs;
us->naddrs = u.naddrs;
+
+#endif
+
us->weight = weight;
us->max_conns = max_conns;
us->max_fails = max_fails;