plugin/reload: fix data races (#2567)

Reload didn't take proper care to protect the fields from use in
different goroutines. Add a mutex and add helpers for usage and
interval.

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2019-02-17 14:57:36 +00:00 committed by GitHub
parent 362d7e96dc
commit a72f3a161c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 13 deletions

View file

@ -25,9 +25,10 @@ func init() {
// 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
var (
r = reload{dur: defaultInterval, u: unused, quit: make(chan bool)}
once, shutOnce sync.Once
)
func setup(c *caddy.Controller) error {
c.Next() // 'reload'
@ -69,8 +70,8 @@ func setup(c *caddy.Controller) error {
i = i + jitter
// prepare info for next onInstanceStartup event
r.interval = i
r.usage = used
r.setInterval(i)
r.setUsage(used)
once.Do(func() {
caddy.RegisterEventHook("reload", hook)