Commit 2716e272 authored by elcore's avatar elcore Committed by Matt Holt

Purge event hooks after USR1 reload, fix #2044 (#2047)

* caddy: Purge event hooks after USR1 reload

* caddy: Remove event hook purge logging

* caddy: Remove deleteEventHook

* caddy: use old event hooks in case of an unsuccessful restart

* caddy: implement restoreEventHooks
parent ca34a3e1
...@@ -39,7 +39,7 @@ var ( ...@@ -39,7 +39,7 @@ var (
// eventHooks is a map of hook name to Hook. All hooks plugins // eventHooks is a map of hook name to Hook. All hooks plugins
// must have a name. // must have a name.
eventHooks = sync.Map{} eventHooks = &sync.Map{}
// parsingCallbacks maps server type to map of directive // parsingCallbacks maps server type to map of directive
// to list of callback functions. These aren't really // to list of callback functions. These aren't really
...@@ -271,6 +271,36 @@ func EmitEvent(event EventName, info interface{}) { ...@@ -271,6 +271,36 @@ func EmitEvent(event EventName, info interface{}) {
}) })
} }
// cloneEventHooks return a clone of the event hooks *sync.Map
func cloneEventHooks() *sync.Map {
c := &sync.Map{}
eventHooks.Range(func(k, v interface{}) bool {
c.Store(k, v)
return true
})
return c
}
// purgeEventHooks purges all event hooks from the map
func purgeEventHooks() {
eventHooks.Range(func(k, _ interface{}) bool {
eventHooks.Delete(k)
return true
})
}
// restoreEventHooks restores eventHooks with a provided *sync.Map
func restoreEventHooks(m *sync.Map) {
// Purge old event hooks
purgeEventHooks()
// Restore event hooks
m.Range(func(k, v interface{}) bool {
eventHooks.Store(k, v)
return true
})
}
// ParsingCallback is a function that is called after // ParsingCallback is a function that is called after
// a directive's setup functions have been executed // a directive's setup functions have been executed
// for all the server blocks. // for all the server blocks.
......
...@@ -76,9 +76,17 @@ func trapSignalsPosix() { ...@@ -76,9 +76,17 @@ func trapSignalsPosix() {
caddyfileToUse = newCaddyfile caddyfileToUse = newCaddyfile
} }
// Backup old event hooks
oldEventHooks := cloneEventHooks()
// Purge the old event hooks
purgeEventHooks()
// Kick off the restart; our work is done // Kick off the restart; our work is done
_, err = inst.Restart(caddyfileToUse) _, err = inst.Restart(caddyfileToUse)
if err != nil { if err != nil {
restoreEventHooks(oldEventHooks)
log.Printf("[ERROR] SIGUSR1: %v", err) log.Printf("[ERROR] SIGUSR1: %v", err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment