forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
91 lines (76 loc) · 1.95 KB
/
setup.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package reload
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/coredns/coredns/plugin"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("reload", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
// the info reload is global to all application, whatever number of reloads.
// it is used to transmit data between Setup and start of the hook called 'onInstanceStartup'
// channel for QUIT is never changed in purpose.
// WARNING: this data may be unsync after an invalid attempt of reload Corefile.
var r = reload{interval: defaultInterval, usage: unused, quit: make(chan bool)}
var once sync.Once
var shutOnce sync.Once
func setup(c *caddy.Controller) error {
c.Next() // 'reload'
args := c.RemainingArgs()
if len(args) > 2 {
return plugin.Error("reload", c.ArgErr())
}
i := defaultInterval
if len(args) > 0 {
d, err := time.ParseDuration(args[0])
if err != nil {
return plugin.Error("reload", err)
}
i = d
}
if i < minInterval {
return plugin.Error("reload", fmt.Errorf("interval value must be greater or equal to %v", minInterval))
}
j := defaultJitter
if len(args) > 1 {
d, err := time.ParseDuration(args[1])
if err != nil {
return plugin.Error("reload", err)
}
j = d
}
if j < minJitter {
return plugin.Error("reload", fmt.Errorf("jitter value must be greater or equal to %v", minJitter))
}
if j > i/2 {
j = i / 2
}
jitter := time.Duration(rand.Int63n(j.Nanoseconds()) - (j.Nanoseconds() / 2))
i = i + jitter
// prepare info for next onInstanceStartup event
r.interval = i
r.usage = used
once.Do(func() {
caddy.RegisterEventHook("reload", hook)
})
// re-register on finalShutDown as the instance most-likely will be changed
shutOnce.Do(func() {
c.OnFinalShutdown(func() error {
r.quit <- true
return nil
})
})
return nil
}
const (
minJitter = 1 * time.Second
minInterval = 2 * time.Second
defaultInterval = 30 * time.Second
defaultJitter = 15 * time.Second
)