diff --git a/cache.go b/cache.go index 2dd7e4b..c390ed9 100644 --- a/cache.go +++ b/cache.go @@ -101,7 +101,7 @@ func (c *cache) get(qname string) RRs { rrs := make(RRs, len(e)) now := time.Now() for rr, _ := range e { - if rr.Expiry != nil && now.After(*rr.Expiry) { + if rr.Expiry != emptyTime && now.After(rr.Expiry) { delete(e, rr) } else { rrs[i] = rr diff --git a/cache_test.go b/cache_test.go index 0d13dd5..f5d39c5 100644 --- a/cache_test.go +++ b/cache_test.go @@ -20,7 +20,7 @@ func TestLiveCacheEntry(t *testing.T) { c := newCache(100, true) c.addNX("alive.") alive := time.Now().Add(time.Minute) - rr := RR{Name: "alive.", Type: "A", Value: "1.2.3.4", Expiry: &alive} + rr := RR{Name: "alive.", Type: "A", Value: "1.2.3.4", Expiry: alive} c.add("alive.", rr) rrs := c.get("alive.") st.Expect(t, len(rrs), 1) @@ -30,7 +30,7 @@ func TestExpiredCacheEntry(t *testing.T) { c := newCache(100, true) c.addNX("expired.") expired := time.Now().Add(-time.Minute) - rr := RR{Name: "expired.", Type: "A", Value: "1.2.3.4", Expiry: &expired} + rr := RR{Name: "expired.", Type: "A", Value: "1.2.3.4", Expiry: expired} c.add("expired.", rr) rrs := c.get("expired.") st.Expect(t, len(rrs), 0) diff --git a/resolver_test.go b/resolver_test.go index bf30ae8..2256a5b 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -193,7 +193,7 @@ func TestTTL(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, len(rrs) >= 4, true) rr := rrs[0] - st.Expect(t, rr.Expiry != nil, true) + st.Expect(t, rr.Expiry != emptyTime, true) } var testResolver *Resolver diff --git a/rr.go b/rr.go index ff326e9..75ea5b4 100644 --- a/rr.go +++ b/rr.go @@ -13,8 +13,8 @@ type RR struct { Name string Type string Value string - TTL *time.Duration - Expiry *time.Time + TTL time.Duration + Expiry time.Time } // RRs represents a slice of DNS resource records. @@ -24,6 +24,9 @@ type RRs []RR // It is used to save allocations at runtime. var emptyRRs = RRs{} +// emptyTime is used to detect an empty expiry time. +var emptyTime = time.Time{} + // ICANN specifies that DNS servers should return the special value 127.0.53.53 // for A record queries of TLDs that have recently entered the root zone, // that have a high likelyhood of colliding with private DNS names. @@ -34,7 +37,7 @@ const NameCollision = "127.0.53.53" // String returns a string representation of an RR in zone-file format. func (rr *RR) String() string { - if rr.TTL == nil { + if rr.Expiry == emptyTime { return rr.Name + "\t 3600\tIN\t" + rr.Type + "\t" + rr.Value } else { ttl := ttlString(rr.TTL) @@ -43,7 +46,7 @@ func (rr *RR) String() string { } // ttlString constructs the TTL field of an RR string. -func ttlString(ttl *time.Duration) string { +func ttlString(ttl time.Duration) string { seconds := int(ttl.Seconds()) return fmt.Sprintf("%10d", seconds) } @@ -53,8 +56,8 @@ func ttlString(ttl *time.Duration) string { // It will attempt to translate this if there are enough parameters // Should all translation fail, it returns an undefined RR and false. func convertRR(drr dns.RR, expire bool) (RR, bool) { - var ttl *time.Duration - var expiry *time.Time + var ttl time.Duration + var expiry time.Time if expire { ttl, expiry = calculateExpiry(drr) } @@ -81,8 +84,8 @@ func convertRR(drr dns.RR, expire bool) (RR, bool) { } // calculateExpiry calculates the expiry time of an RR. -func calculateExpiry(drr dns.RR) (*time.Duration, *time.Time) { +func calculateExpiry(drr dns.RR) (time.Duration, time.Time) { ttl := time.Second * time.Duration(drr.Header().Ttl) expiry := time.Now().Add(ttl) - return &ttl, &expiry + return ttl, expiry }