mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
middleware/cache: cache 0 will be capped at 5 (#408)
* middleware/cache: cache 0 will be capped at 5 cache 0 would return TTL=0 records, up that to the documented minimum of 5 seconds. * middleware/cache: check for 0 TTL Handle 0 TTL differently and return an error, we might need to special case this in the future.
This commit is contained in:
2
middleware/cache/README.md
vendored
2
middleware/cache/README.md
vendored
@@ -14,7 +14,7 @@ cache [TTL] [ZONES...]
|
|||||||
|
|
||||||
Each element in the cache is cached according to its TTL (with **TTL** as the max).
|
Each element in the cache is cached according to its TTL (with **TTL** as the max).
|
||||||
For the negative cache, the SOA's MinTTL value is used. A cache can contain up to 10,000 items by
|
For the negative cache, the SOA's MinTTL value is used. A cache can contain up to 10,000 items by
|
||||||
default.
|
default. A TTL of zero is not allowed.
|
||||||
|
|
||||||
If you want more control:
|
If you want more control:
|
||||||
|
|
||||||
|
|||||||
3
middleware/cache/cache.go
vendored
3
middleware/cache/cache.go
vendored
@@ -125,7 +125,8 @@ func (c *ResponseWriter) Write(buf []byte) (int, error) {
|
|||||||
const (
|
const (
|
||||||
maxTTL = 1 * time.Hour
|
maxTTL = 1 * time.Hour
|
||||||
maxNTTL = 30 * time.Minute
|
maxNTTL = 30 * time.Minute
|
||||||
minTTL = 5 * time.Second
|
|
||||||
|
minTTL = 5 // seconds
|
||||||
|
|
||||||
defaultCap = 10000 // default capacity of the cache.
|
defaultCap = 10000 // default capacity of the cache.
|
||||||
|
|
||||||
|
|||||||
10
middleware/cache/item.go
vendored
10
middleware/cache/item.go
vendored
@@ -63,9 +63,6 @@ func (i *item) toMsg(m *dns.Msg) *dns.Msg {
|
|||||||
m1.Extra = i.Extra
|
m1.Extra = i.Extra
|
||||||
|
|
||||||
ttl := int(i.origTTL) - int(time.Now().UTC().Sub(i.stored).Seconds())
|
ttl := int(i.origTTL) - int(time.Now().UTC().Sub(i.stored).Seconds())
|
||||||
if ttl < int(minTTL.Seconds()) {
|
|
||||||
ttl = int(minTTL.Seconds())
|
|
||||||
}
|
|
||||||
setMsgTTL(m1, uint32(ttl))
|
setMsgTTL(m1, uint32(ttl))
|
||||||
return m1
|
return m1
|
||||||
}
|
}
|
||||||
@@ -75,8 +72,13 @@ func (i *item) expired(now time.Time) bool {
|
|||||||
return ttl < 0
|
return ttl < 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// setMsgTTL sets the ttl on all RRs in all sections.
|
// setMsgTTL sets the ttl on all RRs in all sections. If ttl is smaller than minTTL
|
||||||
|
// that value is used.
|
||||||
func setMsgTTL(m *dns.Msg, ttl uint32) {
|
func setMsgTTL(m *dns.Msg, ttl uint32) {
|
||||||
|
if ttl < minTTL {
|
||||||
|
ttl = minTTL
|
||||||
|
}
|
||||||
|
|
||||||
for _, r := range m.Answer {
|
for _, r := range m.Answer {
|
||||||
r.Header().Ttl = ttl
|
r.Header().Ttl = ttl
|
||||||
}
|
}
|
||||||
|
|||||||
13
middleware/cache/setup.go
vendored
13
middleware/cache/setup.go
vendored
@@ -1,6 +1,7 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -49,6 +50,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
|
|||||||
// first args may be just a number, then it is the ttl, if not it is a zone
|
// first args may be just a number, then it is the ttl, if not it is a zone
|
||||||
ttl, err := strconv.Atoi(args[0])
|
ttl, err := strconv.Atoi(args[0])
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
// Reserve 0 (and smaller for future things)
|
||||||
|
if ttl <= 0 {
|
||||||
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", ttl)
|
||||||
|
}
|
||||||
ca.pttl = time.Duration(ttl) * time.Second
|
ca.pttl = time.Duration(ttl) * time.Second
|
||||||
ca.nttl = time.Duration(ttl) * time.Second
|
ca.nttl = time.Duration(ttl) * time.Second
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
@@ -77,6 +82,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Reserve 0 (and smaller for future things)
|
||||||
|
if pttl <= 0 {
|
||||||
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", pttl)
|
||||||
|
}
|
||||||
ca.pttl = time.Duration(pttl) * time.Second
|
ca.pttl = time.Duration(pttl) * time.Second
|
||||||
}
|
}
|
||||||
case Denial:
|
case Denial:
|
||||||
@@ -94,6 +103,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Reserve 0 (and smaller for future things)
|
||||||
|
if nttl <= 0 {
|
||||||
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", nttl)
|
||||||
|
}
|
||||||
ca.nttl = time.Duration(nttl) * time.Second
|
ca.nttl = time.Duration(nttl) * time.Second
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
5
middleware/cache/setup_test.go
vendored
5
middleware/cache/setup_test.go
vendored
@@ -44,6 +44,11 @@ func TestSetup(t *testing.T) {
|
|||||||
positive 15
|
positive 15
|
||||||
negative aaa
|
negative aaa
|
||||||
}`, true, defaultCap, defaultCap, maxTTL, maxTTL},
|
}`, true, defaultCap, defaultCap, maxTTL, maxTTL},
|
||||||
|
{`cache 0 example.nl`, true, defaultCap, defaultCap, maxTTL, maxTTL},
|
||||||
|
{`cache -1 example.nl`, true, defaultCap, defaultCap, maxTTL, maxTTL},
|
||||||
|
{`cache 1 example.nl {
|
||||||
|
positive 0
|
||||||
|
}`, true, defaultCap, defaultCap, maxTTL, maxTTL},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
c := caddy.NewTestController("dns", test.input)
|
c := caddy.NewTestController("dns", test.input)
|
||||||
|
|||||||
Reference in New Issue
Block a user