From c434e84114ef14b7a53ad25c658aaf5156f6d949 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 22 Dec 2016 07:52:02 -0500 Subject: Adding session cache directly to web-conn (#4861) --- api/context.go | 2 ++ api/post.go | 6 +++--- api/web_conn.go | 35 +++++++++++++++++++++++------------ api/web_hub.go | 6 ++++++ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/api/context.go b/api/context.go index 765bb502a..7f95fdfbc 100644 --- a/api/context.go +++ b/api/context.go @@ -566,6 +566,8 @@ func RemoveAllSessionsForUserIdSkipClusterSend(userId string) { } } + InvalidateWebConnSessionCacheForUser(userId) + } func AddSessionToCache(session *model.Session) { diff --git a/api/post.go b/api/post.go index 00cf21dda..ff3c1e510 100644 --- a/api/post.go +++ b/api/post.go @@ -705,7 +705,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } if userAllowsEmails && status.Status != model.STATUS_ONLINE { - go sendNotificationEmail(c, post, profileMap[id], channel, team, senderName[id], sender) + sendNotificationEmail(c, post, profileMap[id], channel, team, senderName[id], sender) } } } @@ -801,7 +801,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { - go sendPushNotification(post, profileMap[id], channel, senderName[id], true) + sendPushNotification(post, profileMap[id], channel, senderName[id], true) } } @@ -814,7 +814,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { - go sendPushNotification(post, profileMap[id], channel, senderName[id], false) + sendPushNotification(post, profileMap[id], channel, senderName[id], false) } } } diff --git a/api/web_conn.go b/api/web_conn.go index a2b801904..3b991d449 100644 --- a/api/web_conn.go +++ b/api/web_conn.go @@ -27,6 +27,7 @@ type WebConn struct { WebSocket *websocket.Conn Send chan model.WebSocketMessage SessionToken string + SessionExpiresAt int64 UserId string T goi18n.TranslateFunc Locale string @@ -40,12 +41,13 @@ func NewWebConn(c *Context, ws *websocket.Conn) *WebConn { } return &WebConn{ - Send: make(chan model.WebSocketMessage, 256), - WebSocket: ws, - UserId: c.Session.UserId, - SessionToken: c.Session.Token, - T: c.T, - Locale: c.Locale, + Send: make(chan model.WebSocketMessage, 256), + WebSocket: ws, + UserId: c.Session.UserId, + SessionToken: c.Session.Token, + SessionExpiresAt: c.Session.ExpiresAt, + T: c.T, + Locale: c.Locale, } } @@ -144,16 +146,25 @@ func (c *WebConn) writePump() { func (webCon *WebConn) InvalidateCache() { webCon.AllChannelMembers = nil webCon.LastAllChannelMembersTime = 0 + webCon.SessionExpiresAt = 0 } func (webCon *WebConn) isAuthenticated() bool { - if webCon.SessionToken == "" { - return false - } + // Check the expiry to see if we need to check for a new session + if webCon.SessionExpiresAt < model.GetMillis() { + if webCon.SessionToken == "" { + return false + } - session := GetSession(webCon.SessionToken) - if session == nil || session.IsExpired() { - return false + session := GetSession(webCon.SessionToken) + if session == nil || session.IsExpired() { + webCon.SessionToken = "" + webCon.SessionExpiresAt = 0 + return false + } + + webCon.SessionToken = session.Token + webCon.SessionExpiresAt = session.ExpiresAt } return true diff --git a/api/web_hub.go b/api/web_hub.go index 107491434..64903ea59 100644 --- a/api/web_hub.go +++ b/api/web_hub.go @@ -145,6 +145,12 @@ func InvalidateCacheForUserSkipClusterSend(userId string) { } } +func InvalidateWebConnSessionCacheForUser(userId string) { + if len(hubs) != 0 { + GetHubForUserId(userId).InvalidateUser(userId) + } +} + func (h *Hub) Register(webConn *WebConn) { h.register <- webConn -- cgit v1.2.3-1-g7c22