Skip to content

Commit

Permalink
Merge a398c6e into f201eed
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhulen committed Mar 1, 2017
2 parents f201eed + a398c6e commit ba29746
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
25 changes: 17 additions & 8 deletions api/web_conn.go
Expand Up @@ -29,6 +29,7 @@ type WebConn struct {
Send chan model.WebSocketMessage
SessionToken string
SessionExpiresAt int64
Session *model.Session
UserId string
T goi18n.TranslateFunc
Locale string
Expand Down Expand Up @@ -148,6 +149,7 @@ func (webCon *WebConn) InvalidateCache() {
webCon.AllChannelMembers = nil
webCon.LastAllChannelMembersTime = 0
webCon.SessionExpiresAt = 0
webCon.Session = nil
}

func (webCon *WebConn) isAuthenticated() bool {
Expand All @@ -161,11 +163,13 @@ func (webCon *WebConn) isAuthenticated() bool {
if session == nil || session.IsExpired() {
webCon.SessionToken = ""
webCon.SessionExpiresAt = 0
webCon.Session = nil
return false
}

webCon.SessionToken = session.Token
webCon.SessionExpiresAt = session.ExpiresAt
webCon.Session = session
}

return true
Expand Down Expand Up @@ -230,16 +234,21 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
}

func (webCon *WebConn) IsMemberOfTeam(teamId string) bool {
session := GetSession(webCon.SessionToken)
if session == nil {
return false
} else {
member := session.GetTeamByTeamId(teamId)

if member != nil {
return true
} else {
if webCon.Session == nil {
session := GetSession(webCon.SessionToken)
if session == nil {
return false
} else {
webCon.Session = session
}
}

member := webCon.Session.GetTeamByTeamId(teamId)

if member != nil {
return true
} else {
return false
}
}
41 changes: 24 additions & 17 deletions api/web_hub.go
Expand Up @@ -89,6 +89,15 @@ func HubUnregister(webConn *WebConn) {
}

func Publish(message *model.WebSocketEvent) {

if SkipTypingMessage(message) {
if metrics := einterfaces.GetMetricsInterface(); metrics != nil {
metrics.IncrementWebsocketEvent(message.Event + "_skipped")
}

return
}

if metrics := einterfaces.GetMetricsInterface(); metrics != nil {
metrics.IncrementWebsocketEvent(message.Event)
}
Expand Down Expand Up @@ -266,20 +275,18 @@ func (h *Hub) Start() {
}

case msg := <-h.broadcast:
if OkToSendTypingMessage(msg) {
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)
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
}
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)
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
}
}
}
Expand Down Expand Up @@ -319,13 +326,13 @@ func (h *Hub) Start() {
go doRecoverableStart()
}

func OkToSendTypingMessage(msg *model.WebSocketEvent) bool {
func SkipTypingMessage(msg *model.WebSocketEvent) bool {
// Only broadcast typing messages if less than 1K people in channel
if msg.Event == model.WEBSOCKET_EVENT_TYPING {
if Srv.Store.Channel().GetMemberCountFromCache(msg.Broadcast.ChannelId) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel {
return false
return true
}
}

return true
return false
}

0 comments on commit ba29746

Please sign in to comment.