]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/cache: track expiring records in cache
authorMarek Vavruša <marek.vavrusa@nic.cz>
Sun, 5 Jul 2015 20:07:18 +0000 (22:07 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Sun, 5 Jul 2015 20:07:18 +0000 (22:07 +0200)
lib/cache.c
lib/layer/rrcache.c
lib/rplan.h

index afc3eba67dcbafe1304d1d4eba7e2b942e17d5fe..2022d07d7ab6103e48d40be12872d6fb4d51797a 100644 (file)
@@ -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);
                }
index db0687851f6cf03159d0cf2ba7759e68f704feee..9cf0568d7f20b09d656f8e9600559a554d7dac19 100644 (file)
@@ -17,8 +17,9 @@
 #include <libknot/descriptor.h>
 #include <libknot/errcode.h>
 #include <libknot/rrset.h>
-#include <libknot/internal/mempool.h>
 #include <libknot/rrtype/rdname.h>
+#include <ucw/config.h>
+#include <ucw/lib.h>
 
 #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, &timestamp);
+       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) {
index d632fcd13b84add60b3382f7bbe8069248f3b837..ccf300a1baf625eb03fb533a100ea00f09b325ef 100644 (file)
@@ -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 {