2018-01-27 05:42:57 -05:00
|
|
|
package reload
|
|
|
|
|
|
|
|
|
|
import (
|
2018-02-02 13:15:56 -05:00
|
|
|
"fmt"
|
2018-01-27 05:42:57 -05:00
|
|
|
"math/rand"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2020-09-24 18:14:41 +02:00
|
|
|
"github.com/coredns/caddy"
|
2018-01-27 05:42:57 -05:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-04-22 21:40:33 +01:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2018-01-27 05:42:57 -05:00
|
|
|
)
|
|
|
|
|
|
2018-04-22 21:40:33 +01:00
|
|
|
var log = clog.NewWithPlugin("reload")
|
|
|
|
|
|
2019-09-20 08:02:30 +01:00
|
|
|
func init() { plugin.Register("reload", setup) }
|
2018-01-27 05:42:57 -05:00
|
|
|
|
2026-09-07 12:40:07 +10:00
|
|
|
// The event hook is process-global, but reload state belongs to each Caddy instance.
|
|
|
|
|
type reloadStorageKey struct{}
|
|
|
|
|
|
|
|
|
|
func reloadForController(c *caddy.Controller) *reload {
|
|
|
|
|
if state, ok := c.Get(reloadStorageKey{}).(*reload); ok {
|
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state := newReload()
|
|
|
|
|
c.Set(reloadStorageKey{}, state)
|
|
|
|
|
// Stop this instance's watcher on both reload and final shutdown.
|
|
|
|
|
c.OnShutdown(state.shutdown)
|
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var once sync.Once
|
2018-01-27 05:42:57 -05:00
|
|
|
|
|
|
|
|
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 {
|
2018-02-02 13:15:56 -05:00
|
|
|
return plugin.Error("reload", err)
|
2018-01-27 05:42:57 -05:00
|
|
|
}
|
|
|
|
|
i = d
|
|
|
|
|
}
|
2018-02-02 13:15:56 -05:00
|
|
|
if i < minInterval {
|
|
|
|
|
return plugin.Error("reload", fmt.Errorf("interval value must be greater or equal to %v", minInterval))
|
|
|
|
|
}
|
2018-01-27 05:42:57 -05:00
|
|
|
|
|
|
|
|
j := defaultJitter
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
d, err := time.ParseDuration(args[1])
|
|
|
|
|
if err != nil {
|
2018-02-02 13:15:56 -05:00
|
|
|
return plugin.Error("reload", err)
|
2018-01-27 05:42:57 -05:00
|
|
|
}
|
|
|
|
|
j = d
|
|
|
|
|
}
|
2026-03-05 18:35:41 -08:00
|
|
|
|
|
|
|
|
if j != 0 && j < minJitter {
|
|
|
|
|
return plugin.Error("reload", fmt.Errorf("jitter value must be 0 or greater or equal to %v", minJitter))
|
2018-02-02 13:15:56 -05:00
|
|
|
}
|
2018-01-27 05:42:57 -05:00
|
|
|
|
2026-03-05 18:35:41 -08:00
|
|
|
if j > 0 && j > i/2 {
|
2018-01-27 05:42:57 -05:00
|
|
|
j = i / 2
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 18:35:41 -08:00
|
|
|
if j > 0 {
|
|
|
|
|
jitter := time.Duration(rand.Int63n(j.Nanoseconds()) - (j.Nanoseconds() / 2)) // #nosec G404 -- non-cryptographic jitter.
|
|
|
|
|
i = i + jitter
|
|
|
|
|
}
|
2018-01-27 05:42:57 -05:00
|
|
|
|
2018-02-02 13:15:56 -05:00
|
|
|
// prepare info for next onInstanceStartup event
|
2026-09-07 12:40:07 +10:00
|
|
|
state := reloadForController(c)
|
|
|
|
|
state.setInterval(i)
|
|
|
|
|
state.setUsage(used)
|
2018-01-27 05:42:57 -05:00
|
|
|
once.Do(func() {
|
|
|
|
|
caddy.RegisterEventHook("reload", hook)
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
2018-02-02 13:15:56 -05:00
|
|
|
minJitter = 1 * time.Second
|
|
|
|
|
minInterval = 2 * time.Second
|
2018-01-27 05:42:57 -05:00
|
|
|
defaultInterval = 30 * time.Second
|
|
|
|
|
defaultJitter = 15 * time.Second
|
|
|
|
|
)
|