aboutsummaryrefslogtreecommitdiff
path: root/plugin/timeouts/timeouts.go
blob: eea6a64885aa9efcb6b05e3704dc377c8e4f4a8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package timeouts

import (
	"time"

	"github.com/coredns/caddy"
	"github.com/coredns/coredns/core/dnsserver"
	"github.com/coredns/coredns/plugin"
	"github.com/coredns/coredns/plugin/pkg/durations"
)

func init() { plugin.Register("timeouts", setup) }

func setup(c *caddy.Controller) error {
	err := parseTimeouts(c)
	if err != nil {
		return plugin.Error("timeouts", err)
	}
	return nil
}

func parseTimeouts(c *caddy.Controller) error {
	config := dnsserver.GetConfig(c)

	for c.Next() {
		args := c.RemainingArgs()
		if len(args) > 0 {
			return plugin.Error("timeouts", c.ArgErr())
		}

		b := 0
		for c.NextBlock() {
			block := c.Val()
			timeoutArgs := c.RemainingArgs()
			if len(timeoutArgs) != 1 {
				return c.ArgErr()
			}

			timeout, err := durations.NewDurationFromArg(timeoutArgs[0])
			if err != nil {
				return c.Err(err.Error())
			}

			if timeout < (1*time.Second) || timeout > (24*time.Hour) {
				return c.Errf("timeout provided '%s' needs to be between 1 second and 24 hours", timeout)
			}

			switch block {
			case "read":
				config.ReadTimeout = timeout

			case "write":
				config.WriteTimeout = timeout

			case "idle":
				config.IdleTimeout = timeout

			default:
				return c.Errf("unknown option: '%s'", block)
			}
			b++
		}

		if b == 0 {
			return plugin.Error("timeouts", c.Err("timeouts block with no timeouts specified"))
		}
	}
	return nil
}