summaryrefslogtreecommitdiffstats
path: root/app/web_hub.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-10-04 22:48:14 -0700
committerGitHub <noreply@github.com>2017-10-04 22:48:14 -0700
commit2a76eeeeee9acdd9aaebc3ec94a40254b933c966 (patch)
treeec76bae5ee0fe2089e1fcbd50e95165c007e6199 /app/web_hub.go
parentda368539e37c0521cddc673d089bd863d0db3026 (diff)
downloadchat-2a76eeeeee9acdd9aaebc3ec94a40254b933c966.tar.gz
chat-2a76eeeeee9acdd9aaebc3ec94a40254b933c966.tar.bz2
chat-2a76eeeeee9acdd9aaebc3ec94a40254b933c966.zip
fix websocket deadlock (#7577)
Diffstat (limited to 'app/web_hub.go')
-rw-r--r--app/web_hub.go20
1 files changed, 16 insertions, 4 deletions
diff --git a/app/web_hub.go b/app/web_hub.go
index 1525dfbba..0473b29f9 100644
--- a/app/web_hub.go
+++ b/app/web_hub.go
@@ -35,7 +35,7 @@ type Hub struct {
register chan *WebConn
unregister chan *WebConn
broadcast chan *model.WebSocketEvent
- stop chan string
+ stop chan struct{}
didStop chan struct{}
invalidateUser chan string
ExplicitStop bool
@@ -49,7 +49,7 @@ func (a *App) NewWebHub() *Hub {
unregister: make(chan *WebConn, 1),
connections: make([]*WebConn, 0, model.SESSION_CACHE_SIZE),
broadcast: make(chan *model.WebSocketEvent, BROADCAST_QUEUE_SIZE),
- stop: make(chan string),
+ stop: make(chan struct{}),
didStop: make(chan struct{}, 1),
invalidateUser: make(chan string),
ExplicitStop: false,
@@ -324,7 +324,10 @@ func (h *Hub) Register(webConn *WebConn) {
}
func (h *Hub) Unregister(webConn *WebConn) {
- h.unregister <- webConn
+ select {
+ case h.unregister <- webConn:
+ case <-h.stop:
+ }
}
func (h *Hub) Broadcast(message *model.WebSocketEvent) {
@@ -349,7 +352,7 @@ func getGoroutineId() int {
}
func (h *Hub) Stop() {
- h.stop <- "all"
+ close(h.stop)
<-h.didStop
}
@@ -430,9 +433,18 @@ func (h *Hub) Start() {
}
case <-h.stop:
+ userIds := make(map[string]bool)
+
for _, webCon := range h.connections {
+ userIds[webCon.UserId] = true
webCon.Close()
}
+
+ for userId := range userIds {
+ h.app.SetStatusOffline(userId, false)
+ }
+
+ h.connections = make([]*WebConn, 0, model.SESSION_CACHE_SIZE)
h.ExplicitStop = true
h.didStop <- struct{}{}