From 97558f6a6ec4c53fa69035fb430ead209d9c222d Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 13 Jan 2017 13:53:37 -0500 Subject: PLT-4938 Add app package and move logic over from api package (#4931) * Add app package and move logic over from api package * Change app package functions to return errors * Move non-api tests into app package * Fix merge --- app/session.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 app/session.go (limited to 'app/session.go') diff --git a/app/session.go b/app/session.go new file mode 100644 index 000000000..29c961e81 --- /dev/null +++ b/app/session.go @@ -0,0 +1,94 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "github.com/mattermost/platform/einterfaces" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" + "github.com/mattermost/platform/utils" + + l4g "github.com/alecthomas/log4go" +) + +var sessionCache *utils.Cache = utils.NewLru(model.SESSION_CACHE_SIZE) + +func GetSession(token string) (*model.Session, *model.AppError) { + metrics := einterfaces.GetMetricsInterface() + + var session *model.Session + if ts, ok := sessionCache.Get(token); ok { + session = ts.(*model.Session) + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Session") + } + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Session") + } + } + + if session == nil { + if sessionResult := <-Srv.Store.Session().Get(token); sessionResult.Err != nil { + return nil, model.NewLocAppError("GetSession", "api.context.invalid_token.error", map[string]interface{}{"Token": token, "Error": sessionResult.Err.DetailedError}, "") + } else { + session = sessionResult.Data.(*model.Session) + + if session.IsExpired() || session.Token != token { + return nil, model.NewLocAppError("GetSession", "api.context.invalid_token.error", map[string]interface{}{"Token": token, "Error": sessionResult.Err.DetailedError}, "") + } else { + AddSessionToCache(session) + return session, nil + } + } + } + + if session == nil || session.IsExpired() { + return nil, model.NewLocAppError("GetSession", "api.context.invalid_token.error", map[string]interface{}{"Token": token}, "") + } + + return session, nil +} + +func RemoveAllSessionsForUserId(userId string) { + + RemoveAllSessionsForUserIdSkipClusterSend(userId) + + if einterfaces.GetClusterInterface() != nil { + einterfaces.GetClusterInterface().RemoveAllSessionsForUserId(userId) + } +} + +func RemoveAllSessionsForUserIdSkipClusterSend(userId string) { + keys := sessionCache.Keys() + + for _, key := range keys { + if ts, ok := sessionCache.Get(key); ok { + session := ts.(*model.Session) + if session.UserId == userId { + sessionCache.Remove(key) + } + } + } + + InvalidateWebConnSessionCacheForUser(userId) + +} + +func AddSessionToCache(session *model.Session) { + sessionCache.AddWithExpiresInSecs(session.Token, session, int64(*utils.Cfg.ServiceSettings.SessionCacheInMinutes*60)) +} + +func InvalidateAllCaches() { + l4g.Info(utils.T("api.context.invalidate_all_caches")) + sessionCache.Purge() + ClearStatusCache() + store.ClearChannelCaches() + store.ClearUserCaches() + store.ClearPostCaches() +} + +func SessionCacheLength() int { + return sessionCache.Len() +} -- cgit v1.2.3-1-g7c22