2016-08-19 17:14:17 -07:00
|
|
|
package cache
|
2016-04-19 11:13:24 +01:00
|
|
|
|
|
|
|
|
import (
|
2016-11-09 10:01:26 +00:00
|
|
|
"fmt"
|
2016-04-19 11:13:24 +01:00
|
|
|
"strconv"
|
2016-10-02 08:31:44 +01:00
|
|
|
"time"
|
2016-04-19 11:13:24 +01:00
|
|
|
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
|
"github.com/coredns/coredns/middleware"
|
2017-06-13 12:39:10 -07:00
|
|
|
"github.com/coredns/coredns/middleware/pkg/cache"
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-04-19 11:13:24 +01:00
|
|
|
)
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func init() {
|
|
|
|
|
caddy.RegisterPlugin("cache", caddy.Plugin{
|
|
|
|
|
ServerType: "dns",
|
|
|
|
|
Action: setup,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
2016-10-02 08:31:44 +01:00
|
|
|
ca, err := cacheParse(c)
|
2016-04-19 11:13:24 +01:00
|
|
|
if err != nil {
|
2016-09-10 09:16:25 +01:00
|
|
|
return middleware.Error("cache", err)
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2016-09-19 11:26:00 +01:00
|
|
|
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
|
2016-10-02 08:31:44 +01:00
|
|
|
ca.Next = next
|
|
|
|
|
return ca
|
2016-08-19 17:14:17 -07:00
|
|
|
})
|
|
|
|
|
|
2016-10-26 10:01:52 +01:00
|
|
|
// Export the capacity for the metrics. This only happens once, because this is a re-load change only.
|
|
|
|
|
cacheCapacity.WithLabelValues(Success).Set(float64(ca.pcap))
|
|
|
|
|
cacheCapacity.WithLabelValues(Denial).Set(float64(ca.ncap))
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
return nil
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-02 08:31:44 +01:00
|
|
|
func cacheParse(c *caddy.Controller) (*Cache, error) {
|
|
|
|
|
|
2017-06-13 12:39:10 -07:00
|
|
|
ca := &Cache{pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxNTTL, prefetch: 0, duration: 1 * time.Minute}
|
2016-04-19 11:13:24 +01:00
|
|
|
|
|
|
|
|
for c.Next() {
|
2016-10-02 08:31:44 +01:00
|
|
|
// cache [ttl] [zones..]
|
|
|
|
|
origins := make([]string, len(c.ServerBlockKeys))
|
|
|
|
|
copy(origins, c.ServerBlockKeys)
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
// first args may be just a number, then it is the ttl, if not it is a zone
|
|
|
|
|
ttl, err := strconv.Atoi(args[0])
|
|
|
|
|
if err == nil {
|
2016-11-09 10:01:26 +00:00
|
|
|
// Reserve 0 (and smaller for future things)
|
|
|
|
|
if ttl <= 0 {
|
|
|
|
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", ttl)
|
|
|
|
|
}
|
2016-10-02 08:31:44 +01:00
|
|
|
ca.pttl = time.Duration(ttl) * time.Second
|
|
|
|
|
ca.nttl = time.Duration(ttl) * time.Second
|
|
|
|
|
args = args[1:]
|
|
|
|
|
}
|
2016-04-19 11:13:24 +01:00
|
|
|
if len(args) > 0 {
|
2016-10-02 08:31:44 +01:00
|
|
|
copy(origins, args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Refinements? In an extra block.
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
|
switch c.Val() {
|
|
|
|
|
// first number is cap, second is an new ttl
|
2016-10-26 10:01:52 +01:00
|
|
|
case Success:
|
2016-10-02 08:31:44 +01:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
pcap, err := strconv.Atoi(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ca.pcap = pcap
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
pttl, err := strconv.Atoi(args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-11-09 10:01:26 +00:00
|
|
|
// Reserve 0 (and smaller for future things)
|
|
|
|
|
if pttl <= 0 {
|
|
|
|
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", pttl)
|
|
|
|
|
}
|
2016-10-02 08:31:44 +01:00
|
|
|
ca.pttl = time.Duration(pttl) * time.Second
|
|
|
|
|
}
|
2016-10-26 10:01:52 +01:00
|
|
|
case Denial:
|
2016-10-02 08:31:44 +01:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
ncap, err := strconv.Atoi(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ca.ncap = ncap
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
nttl, err := strconv.Atoi(args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2016-11-09 10:01:26 +00:00
|
|
|
// Reserve 0 (and smaller for future things)
|
|
|
|
|
if nttl <= 0 {
|
|
|
|
|
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", nttl)
|
|
|
|
|
}
|
2016-10-02 08:31:44 +01:00
|
|
|
ca.nttl = time.Duration(nttl) * time.Second
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2017-06-13 12:39:10 -07:00
|
|
|
case "prefetch":
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) == 0 || len(args) > 3 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
amount, err := strconv.Atoi(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if amount < 0 {
|
|
|
|
|
return nil, fmt.Errorf("prefetch amount should be positive: %d", amount)
|
|
|
|
|
}
|
|
|
|
|
ca.prefetch = amount
|
|
|
|
|
|
|
|
|
|
ca.duration = 1 * time.Minute
|
|
|
|
|
ca.percentage = 10
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
dur, err := time.ParseDuration(args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ca.duration = dur
|
|
|
|
|
}
|
|
|
|
|
if len(args) > 2 {
|
|
|
|
|
pct := args[2]
|
|
|
|
|
if x := pct[len(pct)-1]; x != '%' {
|
|
|
|
|
return nil, fmt.Errorf("last character of percentage should be `%%`, but is: %q", x)
|
|
|
|
|
}
|
|
|
|
|
pct = pct[:len(pct)-1]
|
|
|
|
|
|
|
|
|
|
num, err := strconv.Atoi(pct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if num < 10 || num > 90 {
|
|
|
|
|
return nil, fmt.Errorf("percentage should fall in range [10, 90]: %d", num)
|
|
|
|
|
}
|
|
|
|
|
ca.percentage = num
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 08:31:44 +01:00
|
|
|
default:
|
|
|
|
|
return nil, c.ArgErr()
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2016-10-02 08:31:44 +01:00
|
|
|
}
|
2016-04-19 11:13:24 +01:00
|
|
|
|
2016-10-02 08:31:44 +01:00
|
|
|
for i := range origins {
|
|
|
|
|
origins[i] = middleware.Host(origins[i]).Normalize()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ca.Zones = origins
|
|
|
|
|
|
2017-06-13 12:39:10 -07:00
|
|
|
ca.pcache = cache.New(ca.pcap)
|
|
|
|
|
ca.ncache = cache.New(ca.ncap)
|
2016-10-02 08:31:44 +01:00
|
|
|
|
|
|
|
|
return ca, nil
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2016-10-02 08:31:44 +01:00
|
|
|
|
|
|
|
|
return nil, nil
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|