From: Marek VavruĊĦa Date: Sun, 5 Jul 2015 20:07:18 +0000 (+0200) Subject: lib/cache: track expiring records in cache X-Git-Tag: v1.0.0-beta1~88^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66cc9ded5d5a1b69d0e4e769ee7e46499778c585;p=thirdparty%2Fknot-resolver.git lib/cache: track expiring records in cache --- diff --git a/lib/cache.c b/lib/cache.c index afc3eba67..2022d07d7 100644 --- a/lib/cache.c +++ b/lib/cache.c @@ -283,7 +283,7 @@ int kr_cache_materialize(knot_rrset_t *dst, const knot_rrset_t *src, uint32_t dr knot_rrset_clear(dst, mm); return kr_error(ENOMEM); } - /* Fixup TTL from absolute time */ + /* Fixup TTL time drift */ rd_dst = knot_rdataset_at(&dst->rrs, dst->rrs.rr_count - 1); knot_rdata_set_ttl(rd_dst, knot_rdata_ttl(rd) - drift); } diff --git a/lib/layer/rrcache.c b/lib/layer/rrcache.c index db0687851..9cf0568d7 100644 --- a/lib/layer/rrcache.c +++ b/lib/layer/rrcache.c @@ -17,8 +17,9 @@ #include #include #include -#include #include +#include +#include #include "lib/layer/iterate.h" #include "lib/cache.h" @@ -36,10 +37,10 @@ static int loot_rr(struct kr_cache_txn *txn, knot_pkt_t *pkt, const knot_dname_t uint16_t rrclass, uint16_t rrtype, struct kr_query *qry) { /* Check if record exists in cache */ - uint32_t timestamp = qry->timestamp.tv_sec; + uint32_t drift = qry->timestamp.tv_sec; knot_rrset_t cache_rr; knot_rrset_init(&cache_rr, (knot_dname_t *)name, rrtype, rrclass); - int ret = kr_cache_peek_rr(txn, &cache_rr, ×tamp); + int ret = kr_cache_peek_rr(txn, &cache_rr, &drift); if (ret != 0) { return ret; } @@ -50,9 +51,14 @@ static int loot_rr(struct kr_cache_txn *txn, knot_pkt_t *pkt, const knot_dname_t knot_pkt_put_question(pkt, qry->sname, qry->sclass, qry->stype); } + /* Mark as expiring if it has less than 5% TTL (or less than 5s) */ + if (100 * (drift + 5) > 95 * knot_rrset_ttl(&cache_rr)) { + qry->flags |= QUERY_EXPIRING; + } + /* Update packet answer */ knot_rrset_t rr_copy; - ret = kr_cache_materialize(&rr_copy, &cache_rr, timestamp, &pkt->mm); + ret = kr_cache_materialize(&rr_copy, &cache_rr, drift, &pkt->mm); if (ret == 0) { ret = knot_pkt_put(pkt, KNOT_COMPR_HINT_QNAME, &rr_copy, KNOT_PF_FREE); if (ret != 0) { diff --git a/lib/rplan.h b/lib/rplan.h index d632fcd13..ccf300a1b 100644 --- a/lib/rplan.h +++ b/lib/rplan.h @@ -35,7 +35,8 @@ X(AWAIT_IPV6 , 1 << 5) /**< Query is waiting for AAAA address. */ \ X(AWAIT_CUT , 1 << 6) /**< Query is waiting for zone cut lookup */ \ X(SAFEMODE , 1 << 7) /**< Don't use fancy stuff (EDNS...) */ \ - X(CACHED , 1 << 8) /**< Query response is cached. */ + X(CACHED , 1 << 8) /**< Query response is cached. */ \ + X(EXPIRING , 1 << 9) /**< Query response is cached, but expiring. */ /** Query flags */ enum kr_query_flag {