summaryrefslogtreecommitdiffstats
path: root/api/web_hub.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/web_hub.go')
-rw-r--r--api/web_hub.go152
1 files changed, 96 insertions, 56 deletions
diff --git a/api/web_hub.go b/api/web_hub.go
index 4a9719d80..5f480880e 100644
--- a/api/web_hub.go
+++ b/api/web_hub.go
@@ -5,6 +5,8 @@ package api
import (
"fmt"
+ "hash/fnv"
+ "runtime"
l4g "github.com/alecthomas/log4go"
@@ -14,27 +16,77 @@ import (
)
type Hub struct {
- connections map[*WebConn]bool
- register chan *WebConn
- unregister chan *WebConn
- broadcast chan *model.WebSocketEvent
- stop chan string
- invalidateUser chan string
- invalidateChannel chan string
+ connections map[*WebConn]bool
+ register chan *WebConn
+ unregister chan *WebConn
+ broadcast chan *model.WebSocketEvent
+ stop chan string
+ invalidateUser chan string
}
-var hub = &Hub{
- register: make(chan *WebConn),
- unregister: make(chan *WebConn),
- connections: make(map[*WebConn]bool),
- broadcast: make(chan *model.WebSocketEvent),
- stop: make(chan string),
- invalidateUser: make(chan string),
- invalidateChannel: make(chan string),
+var hubs []*Hub = make([]*Hub, 0)
+
+func NewWebHub() *Hub {
+ return &Hub{
+ register: make(chan *WebConn),
+ unregister: make(chan *WebConn),
+ connections: make(map[*WebConn]bool, model.SESSION_CACHE_SIZE),
+ broadcast: make(chan *model.WebSocketEvent, 4096),
+ stop: make(chan string),
+ invalidateUser: make(chan string),
+ }
+}
+
+func TotalWebsocketConnections() int {
+ // XXX TODO FIXME, this is racy and needs to be fixed
+ count := 0
+ for _, hub := range hubs {
+ count = count + len(hub.connections)
+ }
+
+ return count
+}
+
+func HubStart() {
+ l4g.Info(utils.T("api.web_hub.start.starting.debug"), runtime.NumCPU()*2)
+
+ // Total number of hubs is twice the number of CPUs.
+ hubs = make([]*Hub, runtime.NumCPU()*2)
+
+ for i := 0; i < len(hubs); i++ {
+ hubs[i] = NewWebHub()
+ hubs[i].Start()
+ }
+}
+
+func HubStop() {
+ l4g.Info(utils.T("api.web_hub.start.stopping.debug"))
+
+ for _, hub := range hubs {
+ hub.Stop()
+ }
+
+ hubs = make([]*Hub, 0)
+}
+
+func HubRegister(webConn *WebConn) {
+ hash := fnv.New32a()
+ hash.Write([]byte(webConn.UserId))
+ index := hash.Sum32() % uint32(len(hubs))
+ hubs[index].Register(webConn)
+}
+
+func HubUnregister(webConn *WebConn) {
+ for _, hub := range hubs {
+ hub.Unregister(webConn)
+ }
}
func Publish(message *model.WebSocketEvent) {
- hub.Broadcast(message)
+ message.DoPreComputeJson()
+ for _, hub := range hubs {
+ hub.Broadcast(message)
+ }
if einterfaces.GetClusterInterface() != nil {
einterfaces.GetClusterInterface().Publish(message)
@@ -42,11 +94,19 @@ func Publish(message *model.WebSocketEvent) {
}
func PublishSkipClusterSend(message *model.WebSocketEvent) {
- hub.Broadcast(message)
+ message.DoPreComputeJson()
+ for _, hub := range hubs {
+ hub.Broadcast(message)
+ }
}
func InvalidateCacheForUser(userId string) {
- hub.invalidateUser <- userId
+
+ Srv.Store.Channel().InvalidateAllChannelMembersForUser(userId)
+
+ for _, hub := range hubs {
+ hub.InvalidateUser(userId)
+ }
if einterfaces.GetClusterInterface() != nil {
einterfaces.GetClusterInterface().InvalidateCacheForUser(userId)
@@ -54,11 +114,17 @@ func InvalidateCacheForUser(userId string) {
}
func InvalidateCacheForChannel(channelId string) {
- hub.invalidateChannel <- channelId
- if einterfaces.GetClusterInterface() != nil {
- einterfaces.GetClusterInterface().InvalidateCacheForChannel(channelId)
- }
+ // XXX TODO FIX ME
+ // This can be removed, but the performance branch
+ // needs to be merged into master so it can be removed
+ // from the enterprise repo as well.
+
+ // hub.invalidateChannel <- channelId
+
+ // if einterfaces.GetClusterInterface() != nil {
+ // einterfaces.GetClusterInterface().InvalidateCacheForChannel(channelId)
+ // }
}
func (h *Hub) Register(webConn *WebConn) {
@@ -79,6 +145,10 @@ func (h *Hub) Broadcast(message *model.WebSocketEvent) {
}
}
+func (h *Hub) InvalidateUser(userId string) {
+ h.invalidateUser <- userId
+}
+
func (h *Hub) Stop() {
h.stop <- "all"
}
@@ -108,6 +178,7 @@ func (h *Hub) Start() {
if !found {
go SetStatusOffline(userId, false)
}
+
case userId := <-h.invalidateUser:
for webCon := range h.connections {
if webCon.UserId == userId {
@@ -115,26 +186,20 @@ func (h *Hub) Start() {
}
}
- case channelId := <-h.invalidateChannel:
- for webCon := range h.connections {
- webCon.InvalidateCacheForChannel(channelId)
- }
-
case msg := <-h.broadcast:
for webCon := range h.connections {
- if shouldSendEvent(webCon, msg) {
+ if webCon.ShouldSendEvent(msg) {
select {
case webCon.Send <- msg:
default:
+ l4g.Error(fmt.Sprintf("webhub.broadcast: cannot send, closing websocket for userId=%v", webCon.UserId))
close(webCon.Send)
delete(h.connections, webCon)
}
}
}
- case s := <-h.stop:
- l4g.Debug(utils.T("api.web_hub.start.stopping.debug"), s)
-
+ case <-h.stop:
for webCon := range h.connections {
webCon.WebSocket.Close()
}
@@ -144,28 +209,3 @@ func (h *Hub) Start() {
}
}()
}
-
-func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool {
- // If the event is destined to a specific user
- if len(msg.Broadcast.UserId) > 0 && webCon.UserId != msg.Broadcast.UserId {
- return false
- }
-
- // if the user is omitted don't send the message
- if _, ok := msg.Broadcast.OmitUsers[webCon.UserId]; ok {
- return false
- }
-
- // Only report events to users who are in the channel for the event
- if len(msg.Broadcast.ChannelId) > 0 {
- return webCon.IsMemberOfChannel(msg.Broadcast.ChannelId)
- }
-
- // Only report events to users who are in the team for the event
- if len(msg.Broadcast.TeamId) > 0 {
- return webCon.IsMemberOfTeam(msg.Broadcast.TeamId)
-
- }
-
- return true
-}