diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-02-27 14:27:15 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-02-27 14:27:15 -0500 |
commit | 72de977c522465bc2ee044cd4a26baf666f299a3 (patch) | |
tree | 2a0e9af53e405b6fc886c82efbf56dfcfe352b7d | |
parent | 4429e2f58c5ff3174c6b745e4c322ad790eb2f05 (diff) | |
download | chat-72de977c522465bc2ee044cd4a26baf666f299a3.tar.gz chat-72de977c522465bc2ee044cd4a26baf666f299a3.tar.bz2 chat-72de977c522465bc2ee044cd4a26baf666f299a3.zip |
Adding recovery to web hub (#5546)
-rw-r--r-- | app/web_hub.go | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/app/web_hub.go b/app/web_hub.go index 13852e8eb..90dfacbdf 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -7,6 +7,7 @@ import ( "fmt" "hash/fnv" "runtime" + "runtime/debug" l4g "github.com/alecthomas/log4go" @@ -22,6 +23,7 @@ type Hub struct { broadcast chan *model.WebSocketEvent stop chan string invalidateUser chan string + ExplicitStop bool } var hubs []*Hub = make([]*Hub, 0) @@ -34,6 +36,7 @@ func NewWebHub() *Hub { broadcast: make(chan *model.WebSocketEvent, 4096), stop: make(chan string), invalidateUser: make(chan string), + ExplicitStop: false, } } @@ -237,7 +240,11 @@ func (h *Hub) Stop() { } func (h *Hub) Start() { - go func() { + var doStart func() + var doRecoverableStart func() + var doRecover func() + + doStart = func() { for { select { case webCon := <-h.register: @@ -305,9 +312,31 @@ func (h *Hub) Start() { for _, webCon := range h.connections { webCon.WebSocket.Close() } + h.ExplicitStop = true return } } - }() + } + + doRecoverableStart = func() { + defer doRecover() + doStart() + } + + doRecover = func() { + if !h.ExplicitStop { + if r := recover(); r != nil { + l4g.Error(fmt.Sprintf("Recovering from Hub panic. Panic was: %v", r)) + } else { + l4g.Error("Webhub stopped unexpectedly. Recovering.") + } + + l4g.Error(string(debug.Stack())) + + go doRecoverableStart() + } + } + + go doRecoverableStart() } |