Skip to content

Commit

Permalink
Changing map to array for web_hub connections. (#5226)
Browse files Browse the repository at this point in the history
  • Loading branch information
crspeller committed Jan 30, 2017
1 parent 39ee573 commit 721ac52
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions app/web_hub.go
Expand Up @@ -16,7 +16,7 @@ import (
)

type Hub struct {
connections map[*WebConn]bool
connections []*WebConn
register chan *WebConn
unregister chan *WebConn
broadcast chan *model.WebSocketEvent
Expand All @@ -30,7 +30,7 @@ func NewWebHub() *Hub {
return &Hub{
register: make(chan *WebConn),
unregister: make(chan *WebConn),
connections: make(map[*WebConn]bool, model.SESSION_CACHE_SIZE),
connections: make([]*WebConn, 0, model.SESSION_CACHE_SIZE),
broadcast: make(chan *model.WebSocketEvent, 4096),
stop: make(chan string),
invalidateUser: make(chan string),
Expand Down Expand Up @@ -213,53 +213,66 @@ func (h *Hub) Start() {
for {
select {
case webCon := <-h.register:
h.connections[webCon] = true
h.connections = append(h.connections, webCon)

case webCon := <-h.unregister:
userId := webCon.UserId
if _, ok := h.connections[webCon]; ok {
delete(h.connections, webCon)
close(webCon.Send)
}

if len(userId) == 0 {
continue
}

found := false
for webCon := range h.connections {
if userId == webCon.UserId {
indexToDel := -1
for i, webConCandidate := range h.connections {
if webConCandidate == webCon {
indexToDel = i
continue
}
if userId == webConCandidate.UserId {
found = true
break
}
}

if indexToDel != -1 {
// Delete the webcon we are unregistering
h.connections[indexToDel] = h.connections[len(h.connections)-1]
h.connections = h.connections[:len(h.connections)-1]
}

if len(userId) == 0 {
continue
}

if !found {
go SetStatusOffline(userId, false)
}

case userId := <-h.invalidateUser:
for webCon := range h.connections {
for _, webCon := range h.connections {
if webCon.UserId == userId {
webCon.InvalidateCache()
}
}

case msg := <-h.broadcast:
for webCon := range h.connections {
for _, webCon := range h.connections {
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)
for i, webConCandidate := range h.connections {
if webConCandidate == webCon {
h.connections[i] = h.connections[len(h.connections)-1]
h.connections = h.connections[:len(h.connections)-1]
break
}
}
}
}
}

case <-h.stop:
for webCon := range h.connections {
for _, webCon := range h.connections {
webCon.WebSocket.Close()
}

Expand Down

0 comments on commit 721ac52

Please sign in to comment.