diff options
author | 2022-12-28 11:14:16 +0000 | |
---|---|---|
committer | 2022-12-28 12:14:16 +0100 | |
commit | e7ad486b50d8f831b7dd4a0840d300a3bcfac471 (patch) | |
tree | c8b4bf4ce26c75b6210ccb594f6c687d66af0fcb /plugin/timeouts/timeouts.go | |
parent | 6c9b49f5c2ab652e0504bfd349815b12a5fb4997 (diff) | |
download | coredns-e7ad486b50d8f831b7dd4a0840d300a3bcfac471.tar.gz coredns-e7ad486b50d8f831b7dd4a0840d300a3bcfac471.tar.zst coredns-e7ad486b50d8f831b7dd4a0840d300a3bcfac471.zip |
plugin/timeouts - Allow ability to configure listening server timeouts (#5784)
Diffstat (limited to 'plugin/timeouts/timeouts.go')
-rw-r--r-- | plugin/timeouts/timeouts.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/plugin/timeouts/timeouts.go b/plugin/timeouts/timeouts.go new file mode 100644 index 000000000..eea6a6488 --- /dev/null +++ b/plugin/timeouts/timeouts.go @@ -0,0 +1,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 +} |