Adding recovery to web hub (#5546)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
4429e2f58c
Коммит
72de977c52
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ type Hub struct {
|
|||||||
broadcast chan *model.WebSocketEvent
|
broadcast chan *model.WebSocketEvent
|
||||||
stop chan string
|
stop chan string
|
||||||
invalidateUser chan string
|
invalidateUser chan string
|
||||||
|
ExplicitStop bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var hubs []*Hub = make([]*Hub, 0)
|
var hubs []*Hub = make([]*Hub, 0)
|
||||||
@@ -34,6 +36,7 @@ func NewWebHub() *Hub {
|
|||||||
broadcast: make(chan *model.WebSocketEvent, 4096),
|
broadcast: make(chan *model.WebSocketEvent, 4096),
|
||||||
stop: make(chan string),
|
stop: make(chan string),
|
||||||
invalidateUser: make(chan string),
|
invalidateUser: make(chan string),
|
||||||
|
ExplicitStop: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +240,11 @@ func (h *Hub) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) Start() {
|
func (h *Hub) Start() {
|
||||||
go func() {
|
var doStart func()
|
||||||
|
var doRecoverableStart func()
|
||||||
|
var doRecover func()
|
||||||
|
|
||||||
|
doStart = func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case webCon := <-h.register:
|
case webCon := <-h.register:
|
||||||
@@ -305,9 +312,31 @@ func (h *Hub) Start() {
|
|||||||
for _, webCon := range h.connections {
|
for _, webCon := range h.connections {
|
||||||
webCon.WebSocket.Close()
|
webCon.WebSocket.Close()
|
||||||
}
|
}
|
||||||
|
h.ExplicitStop = true
|
||||||
|
|
||||||
return
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user