Skip to content

Commit

Permalink
added changes to do less i/o to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
dencoded committed May 16, 2018
1 parent e7f602e commit fc5cfac
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
37 changes: 31 additions & 6 deletions auth_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ type DefaultAuthorisationManager struct {
}

type DefaultSessionManager struct {
store storage.Handler
asyncWrites bool
store storage.Handler
asyncWrites bool
disableCacheSessionState bool
}

func (b *DefaultAuthorisationManager) Init(store storage.Handler) {
Expand Down Expand Up @@ -85,6 +86,7 @@ func (b *DefaultAuthorisationManager) KeyExpired(newSession *user.SessionState)

func (b *DefaultSessionManager) Init(store storage.Handler) {
b.asyncWrites = config.Global().UseAsyncSessionWrite
b.disableCacheSessionState = config.Global().LocalSessionCache.DisableCacheSessionState
b.store = store
b.store.Connect()
}
Expand Down Expand Up @@ -120,22 +122,45 @@ func (b *DefaultSessionManager) UpdateSession(keyName string, session *user.Sess

v, _ := json.Marshal(session)

// Keep the TTL
if hashed {
keyName = b.store.GetKeyPrefix() + keyName
}

// async update and return if needed
if b.asyncWrites {
b.renewSessionState(keyName, session)

if hashed {
go b.store.SetRawKey(b.store.GetKeyPrefix()+keyName, string(v), resetTTLTo)
go b.store.SetRawKey(keyName, string(v), resetTTLTo)
return nil
}

go b.store.SetKey(keyName, string(v), resetTTLTo)
return nil
}

// sync update
var err error
if hashed {
return b.store.SetRawKey(b.store.GetKeyPrefix()+keyName, string(v), resetTTLTo)
err = b.store.SetRawKey(keyName, string(v), resetTTLTo)
} else {
err = b.store.SetKey(keyName, string(v), resetTTLTo)
}

return b.store.SetKey(keyName, string(v), resetTTLTo)
if err == nil {
b.renewSessionState(keyName, session)
}

return err
}

func (b *DefaultSessionManager) renewSessionState(keyName string, session *user.SessionState) {
// we have new session state so renew first-seen hash to prevent
session.SetFirstSeenHash()
// delete it from session cache to have it re-populated next time
if !b.disableCacheSessionState {
SessionCache.Delete(keyName)
}
}

// RemoveSession removes session from storage
Expand Down
13 changes: 10 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,17 @@ func (t BaseMiddleware) ApplyPolicies(key string, session *user.SessionState) er
tags[tag] = true
}
}
session.Tags = make([]string, 0, len(tags))
for tag := range tags {
session.Tags = append(session.Tags, tag)

// set tags
if len(tags) > 0 {
session.Tags = make([]string, 0, len(tags))
for tag := range tags {
session.Tags = append(session.Tags, tag)
}
} else {
session.Tags = nil
}

session.AccessRights = rights
// Update the session in the session manager in case it gets called again
return t.Spec.SessionManager.UpdateSession(key, session, session.Lifetime(t.Spec.SessionLifetime), false)
Expand Down
13 changes: 7 additions & 6 deletions user/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ type SessionState struct {
IdExtractorDeadline int64 `json:"id_extractor_deadline" msg:"id_extractor_deadline"`
SessionLifetime int64 `bson:"session_lifetime" json:"session_lifetime"`

FirstSeenHash string `bson:"-" json:"-"`
firstSeenHash string
}

var murmurHasher = murmur3.New32()

func (s *SessionState) SetFirstSeenHash() {
s.FirstSeenHash = s.Hash()
s.firstSeenHash = s.Hash()
}

func (s *SessionState) Hash() string {
Expand All @@ -87,11 +85,14 @@ func (s *SessionState) Hash() string {
log.Error("Error encoding session data: ", err)
return ""
}
return string(murmurHasher.Sum(encoded)[:])
murmurHasher := murmur3.New32()
murmurHasher.Write(encoded)
murmurHasher.Sum32()
return string(murmurHasher.Sum(nil)[:])
}

func (s *SessionState) HasChanged() bool {
return s.FirstSeenHash == "" || s.FirstSeenHash != s.Hash()
return s.firstSeenHash == "" || s.firstSeenHash != s.Hash()
}

func (s *SessionState) Lifetime(fallback int64) int64 {
Expand Down

0 comments on commit fc5cfac

Please sign in to comment.