Skip to content

Commit

Permalink
Now nginx uses TTL of a DNS response when calculating cache validity.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdocguard committed Nov 16, 2011
1 parent 963315a commit bec516b
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/core/ngx_resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
{
ngx_str_t s;
ngx_url_t u;
ngx_uint_t i;
ngx_resolver_t *r;
Expand Down Expand Up @@ -146,12 +147,27 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)

r->resend_timeout = 5;
r->expire = 30;
r->valid = 300;
r->valid = 0;

r->log = &cf->cycle->new_log;
r->log_level = NGX_LOG_ERR;

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));

u.host = names[i];
Expand Down Expand Up @@ -1149,6 +1165,7 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,
char *err;
u_char *cname;
size_t len;
int32_t ttl;
uint32_t hash;
in_addr_t addr, *addrs;
ngx_str_t name;
Expand Down Expand Up @@ -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;
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) {

Expand Down Expand Up @@ -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,
"resolver naddrs:%ui cname:%p", naddrs, cname);
ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
"resolver naddrs:%ui cname:%p ttl:%d",
naddrs, cname, ttl);

if (naddrs) {

Expand Down Expand Up @@ -1357,7 +1381,7 @@ ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last,

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;

ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
Expand Down Expand Up @@ -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->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;

ngx_queue_insert_head(&r->name_expire_queue, &rn->queue);
Expand Down Expand Up @@ -1450,6 +1475,7 @@ ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,
char *err;
size_t len;
in_addr_t addr;
int32_t ttl;
ngx_int_t digit;
ngx_str_t name;
ngx_uint_t i, mask, qident;
Expand Down Expand Up @@ -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];

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,
"resolver qt:%ui cl:%ui len:%uz",
Expand Down Expand Up @@ -1581,7 +1613,7 @@ ngx_resolver_process_ptr(ngx_resolver_t *r, u_char *buf, size_t n,

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;

ngx_queue_insert_head(&r->addr_expire_queue, &rn->queue);
Expand Down

0 comments on commit bec516b

Please sign in to comment.