Now nginx uses TTL of a DNS response when calculating cache validity.

Previously it used a hardcoded value of 300 seconds.  Also added the
"valid=" parameter to the "resolver" directive that can be used to
override the cache validity time.

Patch by Kirill A. Korinskiy with minor changes.
This commit is contained in:
Ruslan Ermilov 2011-11-16 13:11:39 +00:00
parent 963315aca0
commit bec516bec9

View File

@ -93,6 +93,7 @@ static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len);
ngx_resolver_t * ngx_resolver_t *
ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
{ {
ngx_str_t s;
ngx_url_t u; ngx_url_t u;
ngx_uint_t i; ngx_uint_t i;
ngx_resolver_t *r; ngx_resolver_t *r;
@ -146,12 +147,27 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
r->resend_timeout = 5; r->resend_timeout = 5;
r->expire = 30; r->expire = 30;
r->valid = 300; r->valid = 0;
r->log = &cf->cycle->new_log; r->log = &cf->cycle->new_log;
r->log_level = NGX_LOG_ERR; r->log_level = NGX_LOG_ERR;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (ngx_strncmp(names[i].data, "valid=", 6) == 0) {
s.len = names[i].len - 6;
s.data = names[i].data + 6;
r->valid = ngx_parse_time(&s, 1);
if (r->valid == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter: %V", &names[i]);
return NULL;
}
continue;
}
ngx_memzero(&u, sizeof(ngx_url_t)); ngx_memzero(&u, sizeof(ngx_url_t));
u.host = names[i]; u.host = names[i];
@ -1149,6 +1165,7 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
char *err; char *err;
u_char *cname; u_char *cname;
size_t len; size_t len;
int32_t ttl;
uint32_t hash; uint32_t hash;
in_addr_t addr, *addrs; in_addr_t addr, *addrs;
ngx_str_t name; ngx_str_t name;
@ -1258,6 +1275,12 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
qtype = (an->type_hi << 8) + an->type_lo; qtype = (an->type_hi << 8) + an->type_lo;
len = (an->len_hi << 8) + an->len_lo; len = (an->len_hi << 8) + an->len_lo;
ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
+ (an->ttl[2] << 8) + (an->ttl[3]);
if (ttl < 0) {
ttl = 0;
}
if (qtype == NGX_RESOLVE_A) { if (qtype == NGX_RESOLVE_A) {
@ -1287,8 +1310,9 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
} }
} }
ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
"resolver naddrs:%ui cname:%p", naddrs, cname); "resolver naddrs:%ui cname:%p ttl:%d",
naddrs, cname, ttl);
if (naddrs) { if (naddrs) {
@ -1357,7 +1381,7 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
ngx_queue_remove(&rn->queue); ngx_queue_remove(&rn->queue);
rn->valid = ngx_time() + r->valid; rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
rn->expire = ngx_time() + r->expire; rn->expire = ngx_time() + r->expire;
ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
@ -1399,7 +1423,8 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
rn->cnlen = (u_short) name.len; rn->cnlen = (u_short) name.len;
rn->u.cname = name.data; rn->u.cname = name.data;
rn->valid = ngx_time() + r->valid;
rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
rn->expire = ngx_time() + r->expire; rn->expire = ngx_time() + r->expire;
ngx_queue_insert_head(&r->name_expire_queue, &rn->queue); ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
@ -1450,6 +1475,7 @@ ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
char *err; char *err;
size_t len; size_t len;
in_addr_t addr; in_addr_t addr;
int32_t ttl;
ngx_int_t digit; ngx_int_t digit;
ngx_str_t name; ngx_str_t name;
ngx_uint_t i, mask, qident; ngx_uint_t i, mask, qident;
@ -1545,6 +1571,12 @@ ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
an = (ngx_resolver_an_t *) &buf[i + 2]; an = (ngx_resolver_an_t *) &buf[i + 2];
len = (an->len_hi << 8) + an->len_lo; len = (an->len_hi << 8) + an->len_lo;
ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
+ (an->ttl[2] << 8) + (an->ttl[3]);
if (ttl < 0) {
ttl = 0;
}
ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0, ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
"resolver qt:%ui cl:%ui len:%uz", "resolver qt:%ui cl:%ui len:%uz",
@ -1581,7 +1613,7 @@ ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
ngx_queue_remove(&rn->queue); ngx_queue_remove(&rn->queue);
rn->valid = ngx_time() + r->valid; rn->valid = ngx_time() + (r->valid ? r->valid : ttl);
rn->expire = ngx_time() + r->expire; rn->expire = ngx_time() + r->expire;
ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue); ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);