From 494dbc4cf5519eadeba4b5787edd73ab27ff9bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Fri, 1 Jun 2018 05:59:13 +0200 Subject: [PATCH] Remove session expiration timer --- pkg/session/session.go | 31 +++++++++++-------------------- server/state.go | 1 - 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/pkg/session/session.go b/pkg/session/session.go index dedce0c2..12ffb8a6 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -18,10 +18,9 @@ var ( type Session struct { UserID uint64 - key string - createdAt int64 - expiration *time.Timer - lock sync.Mutex + key string + createdAt int64 + lock sync.Mutex } func New(id uint64) (*Session, error) { @@ -31,18 +30,12 @@ func New(id uint64) (*Session, error) { } return &Session{ - key: key, - createdAt: time.Now().Unix(), - UserID: id, - expiration: time.NewTimer(Expiration), + key: key, + createdAt: time.Now().Unix(), + UserID: id, }, nil } -func (s *Session) Init() { - exp := time.Until(time.Unix(s.createdAt, 0).Add(Expiration)) - s.expiration = time.NewTimer(exp) -} - func (s *Session) Key() string { s.lock.Lock() key := s.key @@ -51,11 +44,15 @@ func (s *Session) Key() string { } func (s *Session) SetCookie(w http.ResponseWriter, r *http.Request) { + s.lock.Lock() + created := time.Unix(s.createdAt, 0) + s.lock.Unlock() + http.SetCookie(w, &http.Cookie{ Name: CookieName, Value: s.Key(), Path: "/", - Expires: time.Now().Add(Expiration), + Expires: created.Add(Expiration), HttpOnly: true, Secure: r.TLS != nil, }) @@ -83,8 +80,6 @@ func (s *Session) Refresh() (string, bool, error) { return "", false, err } - s.expiration.Reset(Expiration) - s.lock.Lock() s.createdAt = time.Now().Unix() s.key = key @@ -95,10 +90,6 @@ func (s *Session) Refresh() (string, bool, error) { return "", false, nil } -func (s *Session) WaitUntilExpiration() { - <-s.expiration.C -} - func newSessionKey() (string, error) { key := make([]byte, 32) _, err := rand.Read(key) diff --git a/server/state.go b/server/state.go index df650dda..a23b6f3c 100644 --- a/server/state.go +++ b/server/state.go @@ -248,7 +248,6 @@ func newStateStore(sessionStore storage.SessionStore) *stateStore { for _, session := range sessions { if !session.Expired() { - session.Init() store.sessions[session.Key()] = session } else { go sessionStore.DeleteSession(session.Key())