coredns/middleware/erratic/setup.go
Miek Gieben a83d97a5c4 middleware/erratic: add delaying queries (#614)
* middleware/erratic: add delying queries

* Dont println
2017-04-13 16:26:17 +01:00

90 lines
1.7 KiB
Go

package erratic
import (
"fmt"
"strconv"
"time"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/middleware"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("erratic", caddy.Plugin{
ServerType: "dns",
Action: setupErratic,
})
}
func setupErratic(c *caddy.Controller) error {
e, err := parseErratic(c)
if err != nil {
return middleware.Error("erratic", err)
}
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
return e
})
return nil
}
func parseErratic(c *caddy.Controller) (*Erratic, error) {
e := &Erratic{drop: 2}
for c.Next() { // 'erratic'
for c.NextBlock() {
switch c.Val() {
case "drop":
args := c.RemainingArgs()
if len(args) > 1 {
return nil, c.ArgErr()
}
if len(args) == 0 {
continue
}
amount, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return nil, err
}
if amount < 0 {
return nil, fmt.Errorf("illegal amount value given %q", args[0])
}
e.drop = uint64(amount)
case "delay":
args := c.RemainingArgs()
if len(args) > 2 {
return nil, c.ArgErr()
}
// Defaults.
e.delay = 2
e.duration = time.Duration(100 * time.Millisecond)
if len(args) == 0 {
continue
}
amount, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return nil, err
}
if amount < 0 {
return nil, fmt.Errorf("illegal amount value given %q", args[0])
}
e.delay = uint64(amount)
if len(args) > 1 {
duration, err := time.ParseDuration(args[1])
if err != nil {
return nil, err
}
e.duration = duration
}
}
}
}
return e, nil
}