summaryrefslogtreecommitdiffstats
path: root/app/web_hub.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2018-05-10 11:22:10 -0400
committerDerrick Anderson <derrick@andersonwebstudio.com>2018-05-10 11:22:10 -0400
commit68340d4715ba470462c4e870f824533f6559c6f1 (patch)
tree2d137d1f0a282c7de372735a3f3587636ecb1c7b /app/web_hub.go
parent823b22c403510a52d56bc96428cf9977b80a9dfc (diff)
downloadchat-68340d4715ba470462c4e870f824533f6559c6f1.tar.gz
chat-68340d4715ba470462c4e870f824533f6559c6f1.tar.bz2
chat-68340d4715ba470462c4e870f824533f6559c6f1.zip
Prevent divide by zero if there are no hubs (#8763)
Diffstat (limited to 'app/web_hub.go')
-rw-r--r--app/web_hub.go14
1 files changed, 12 insertions, 2 deletions
diff --git a/app/web_hub.go b/app/web_hub.go
index 18eb97c8e..c9ca2f4f5 100644
--- a/app/web_hub.go
+++ b/app/web_hub.go
@@ -125,6 +125,10 @@ func (a *App) HubStop() {
}
func (a *App) GetHubForUserId(userId string) *Hub {
+ if len(a.Hubs) == 0 {
+ return nil
+ }
+
hash := fnv.New32a()
hash.Write([]byte(userId))
index := hash.Sum32() % uint32(len(a.Hubs))
@@ -132,11 +136,17 @@ func (a *App) GetHubForUserId(userId string) *Hub {
}
func (a *App) HubRegister(webConn *WebConn) {
- a.GetHubForUserId(webConn.UserId).Register(webConn)
+ hub := a.GetHubForUserId(webConn.UserId)
+ if hub != nil {
+ hub.Register(webConn)
+ }
}
func (a *App) HubUnregister(webConn *WebConn) {
- a.GetHubForUserId(webConn.UserId).Unregister(webConn)
+ hub := a.GetHubForUserId(webConn.UserId)
+ if hub != nil {
+ hub.Unregister(webConn)
+ }
}
func (a *App) Publish(message *model.WebSocketEvent) {