Upstream: per-upstream resolver.

The "resolver" and "resolver_timeout" directives can now be specified
directly in the "upstream" block.
This commit is contained in:
Vladimir Homutov 2019-10-18 16:33:15 +03:00 committed by Aleksei Bavshin
parent 5ebe7a4122
commit ea4654550a
4 changed files with 115 additions and 12 deletions

View File

@ -169,6 +169,10 @@ static ngx_int_t ngx_http_upstream_cookie_variable(ngx_http_request_t *r,
static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#if (NGX_HTTP_UPSTREAM_ZONE)
static char *ngx_http_upstream_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#endif
static ngx_int_t ngx_http_upstream_set_local(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_http_upstream_local_t *local);
@ -339,6 +343,24 @@ static ngx_command_t ngx_http_upstream_commands[] = {
0,
NULL },
#if (NGX_HTTP_UPSTREAM_ZONE)
{ ngx_string("resolver"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_resolver,
NGX_HTTP_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("resolver_timeout"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_upstream_srv_conf_t, resolver_timeout),
NULL },
#endif
ngx_null_command
};
@ -6434,6 +6456,32 @@ not_supported:
}
#if (NGX_HTTP_UPSTREAM_ZONE)
static char *
ngx_http_upstream_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_upstream_srv_conf_t *uscf = conf;
ngx_str_t *value;
if (uscf->resolver) {
return "is duplicate";
}
value = cf->args->elts;
uscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
if (uscf->resolver == NULL) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
#endif
ngx_http_upstream_srv_conf_t *
ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
{
@ -6515,6 +6563,9 @@ ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
uscf->line = cf->conf_file->line;
uscf->port = u->port;
uscf->no_port = u->no_port;
#if (NGX_HTTP_UPSTREAM_ZONE)
uscf->resolver_timeout = NGX_CONF_UNSET_MSEC;
#endif
if (u->naddrs == 1 && (u->port || u->family == AF_UNIX)) {
uscf->servers = ngx_array_create(cf->pool, 1,

View File

@ -97,15 +97,15 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
us->resolver = clcf->resolver;
us->resolver_timeout = clcf->resolver_timeout;
if (us->resolver == NULL) {
us->resolver = clcf->resolver;
}
/*
* Without "resolver_timeout" in http{}, the value is unset.
* Even if we set it in ngx_http_core_merge_loc_conf(), it's
* still dependent on the module order and unreliable.
* Without "resolver_timeout" in http{} the merged value is unset.
*/
ngx_conf_init_msec_value(us->resolver_timeout, 30000);
ngx_conf_merge_msec_value(us->resolver_timeout,
clcf->resolver_timeout, 30000);
if (resolve
&& (us->resolver == NULL

View File

@ -22,6 +22,11 @@ static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd,
void *dummy);
static char *ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#if (NGX_STREAM_UPSTREAM_ZONE)
static char *ngx_stream_upstream_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#endif
static void *ngx_stream_upstream_create_main_conf(ngx_conf_t *cf);
static char *ngx_stream_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
@ -42,6 +47,24 @@ static ngx_command_t ngx_stream_upstream_commands[] = {
0,
NULL },
#if (NGX_STREAM_UPSTREAM_ZONE)
{ ngx_string("resolver"),
NGX_STREAM_UPS_CONF|NGX_CONF_1MORE,
ngx_stream_upstream_resolver,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("resolver_timeout"),
NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_upstream_srv_conf_t, resolver_timeout),
NULL },
#endif
ngx_null_command
};
@ -661,6 +684,32 @@ not_supported:
}
#if (NGX_STREAM_UPSTREAM_ZONE)
static char *
ngx_stream_upstream_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_stream_upstream_srv_conf_t *uscf = conf;
ngx_str_t *value;
if (uscf->resolver) {
return "is duplicate";
}
value = cf->args->elts;
uscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
if (uscf->resolver == NULL) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
#endif
ngx_stream_upstream_srv_conf_t *
ngx_stream_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
{
@ -739,6 +788,9 @@ ngx_stream_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
uscf->line = cf->conf_file->line;
uscf->port = u->port;
uscf->no_port = u->no_port;
#if (NGX_STREAM_UPSTREAM_ZONE)
uscf->resolver_timeout = NGX_CONF_UNSET_MSEC;
#endif
if (u->naddrs == 1 && (u->port || u->family == AF_UNIX)) {
uscf->servers = ngx_array_create(cf->pool, 1,

View File

@ -104,15 +104,15 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
cscf = ngx_stream_conf_get_module_srv_conf(cf,
ngx_stream_core_module);
us->resolver = cscf->resolver;
us->resolver_timeout = cscf->resolver_timeout;
if (us->resolver == NULL) {
us->resolver = cscf->resolver;
}
/*
* Without "resolver_timeout" in stream{}, the value is unset.
* Even if we set it in ngx_stream_core_merge_srv_conf(), it's
* still dependent on the module order and unreliable.
* Without "resolver_timeout" in stream{} the merged value is unset.
*/
ngx_conf_init_msec_value(us->resolver_timeout, 30000);
ngx_conf_merge_msec_value(us->resolver_timeout,
cscf->resolver_timeout, 30000);
if (resolve
&& (us->resolver == NULL