pforemski/dingo

fix caching

Opened this issue · 1 comments

Currently, dingo just caches DNS rrsets for 10 seconds, which generally isn't a bug, but is largely suboptimal in many cases.

Implemented TTLs in cache like this seems to work,
I wish there was a way that didn't involve looping through all answers

        /* check cache */
        var r Reply
        cid := fmt.Sprintf("%s/%d", qname, qtype)
        if x, found := rcache.Get(cid); found {
                r = x.(Reply)
                dbg(8, "cache hit: %+v", r)
                /* Just because we get a hit doesn't mean we should refresh TTL */
        } else {
                /* pass to resolvers and block until the response comes */
                r = resolve(qname, int(qtype))
                dbg(8, "got reply: %+v", r)

                if (r.Status >= 0) {
                        var lowestTtl uint32 = 3600
                        for _,grr := range r.Answer {
                                if grr.TTL < lowestTtl {
                                        lowestTtl = grr.TTL
                                }
                        }
                        for _,grr := range r.Authority {
                                if grr.TTL < lowestTtl {
                                        lowestTtl = grr.TTL
                                }
                        }
                        dbg(4, "Updating cache: TTL: %d, payload: %+v", lowestTtl, r)
                        /* Update Cache, and use real TTL as timeout */
                        rcache.Set(cid, r, time.Duration(lowestTtl)*time.Second)
                }
        }