Migrate to idiomatic error handling in app/web_conn.go (#9709)

Этот коммит содержится в:
Hanzei
2018-10-19 14:31:20 +02:00
коммит произвёл George Goldberg
родитель c3993704ef
Коммит 2c849c7998

Просмотреть файл

@@ -8,10 +8,9 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/gorilla/websocket"
"github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/gorilla/websocket"
goi18n "github.com/nicksnyder/go-i18n/i18n" goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
@@ -143,11 +142,9 @@ func (c *WebConn) readPump() {
} else { } else {
mlog.Debug(fmt.Sprintf("websocket.read: closing websocket for userId=%v error=%v", c.UserId, err.Error())) mlog.Debug(fmt.Sprintf("websocket.read: closing websocket for userId=%v error=%v", c.UserId, err.Error()))
} }
return return
} else {
c.App.Srv.WebSocketRouter.ServeWebSocket(c, &req)
} }
c.App.Srv.WebSocketRouter.ServeWebSocket(c, &req)
} }
} }
@@ -211,7 +208,6 @@ func (c *WebConn) writePump() {
} else { } else {
mlog.Debug(fmt.Sprintf("websocket.send: closing websocket for userId=%v, error=%v", c.UserId, err.Error())) mlog.Debug(fmt.Sprintf("websocket.send: closing websocket for userId=%v, error=%v", c.UserId, err.Error()))
} }
return return
} }
@@ -220,8 +216,8 @@ func (c *WebConn) writePump() {
c.App.Metrics.IncrementWebSocketBroadcast(msg.EventType()) c.App.Metrics.IncrementWebSocketBroadcast(msg.EventType())
}) })
} }
} }
case <-ticker.C: case <-ticker.C:
c.WebSocket.SetWriteDeadline(time.Now().Add(WRITE_WAIT)) c.WebSocket.SetWriteDeadline(time.Now().Add(WRITE_WAIT))
if err := c.WebSocket.WriteMessage(websocket.PingMessage, []byte{}); err != nil { if err := c.WebSocket.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
@@ -231,11 +227,12 @@ func (c *WebConn) writePump() {
} else { } else {
mlog.Debug(fmt.Sprintf("websocket.ticker: closing websocket for userId=%v error=%v", c.UserId, err.Error())) mlog.Debug(fmt.Sprintf("websocket.ticker: closing websocket for userId=%v error=%v", c.UserId, err.Error()))
} }
return return
} }
case <-c.endWritePump: case <-c.endWritePump:
return return
case <-authTicker.C: case <-authTicker.C:
if c.GetSessionToken() == "" { if c.GetSessionToken() == "" {
mlog.Debug(fmt.Sprintf("websocket.authTicker: did not authenticate ip=%v", c.WebSocket.RemoteAddr())) mlog.Debug(fmt.Sprintf("websocket.authTicker: did not authenticate ip=%v", c.WebSocket.RemoteAddr()))
@@ -314,9 +311,8 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
if len(msg.Broadcast.UserId) > 0 { if len(msg.Broadcast.UserId) > 0 {
if webCon.UserId == msg.Broadcast.UserId { if webCon.UserId == msg.Broadcast.UserId {
return true return true
} else {
return false
} }
return false
} }
// if the user is omitted don't send the message // if the user is omitted don't send the message
@@ -334,33 +330,30 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
} }
if webCon.AllChannelMembers == nil { if webCon.AllChannelMembers == nil {
if result := <-webCon.App.Srv.Store.Channel().GetAllChannelMembersForUser(webCon.UserId, true, false); result.Err != nil { result := <-webCon.App.Srv.Store.Channel().GetAllChannelMembersForUser(webCon.UserId, true, false)
if result.Err != nil {
mlog.Error("webhub.shouldSendEvent: " + result.Err.Error()) mlog.Error("webhub.shouldSendEvent: " + result.Err.Error())
return false return false
} else {
webCon.AllChannelMembers = result.Data.(map[string]string)
webCon.LastAllChannelMembersTime = model.GetMillis()
} }
webCon.AllChannelMembers = result.Data.(map[string]string)
webCon.LastAllChannelMembersTime = model.GetMillis()
} }
if _, ok := webCon.AllChannelMembers[msg.Broadcast.ChannelId]; ok { if _, ok := webCon.AllChannelMembers[msg.Broadcast.ChannelId]; ok {
return true return true
} else {
return false
} }
return false
} }
// Only report events to users who are in the team for the event // Only report events to users who are in the team for the event
if len(msg.Broadcast.TeamId) > 0 { if len(msg.Broadcast.TeamId) > 0 {
return webCon.IsMemberOfTeam(msg.Broadcast.TeamId) return webCon.IsMemberOfTeam(msg.Broadcast.TeamId)
} }
return true return true
} }
func (webCon *WebConn) IsMemberOfTeam(teamId string) bool { func (webCon *WebConn) IsMemberOfTeam(teamId string) bool {
currentSession := webCon.GetSession() currentSession := webCon.GetSession()
if currentSession == nil || len(currentSession.Token) == 0 { if currentSession == nil || len(currentSession.Token) == 0 {
@@ -368,17 +361,15 @@ func (webCon *WebConn) IsMemberOfTeam(teamId string) bool {
if err != nil { if err != nil {
mlog.Error(fmt.Sprintf("Invalid session err=%v", err.Error())) mlog.Error(fmt.Sprintf("Invalid session err=%v", err.Error()))
return false return false
} else {
webCon.SetSession(session)
currentSession = session
} }
webCon.SetSession(session)
currentSession = session
} }
member := currentSession.GetTeamByTeamId(teamId) member := currentSession.GetTeamByTeamId(teamId)
if member != nil { if member != nil {
return true return true
} else {
return false
} }
return false
} }