Remove session expiration timer
This commit is contained in:
parent
c49bbc72d4
commit
494dbc4cf5
@ -18,10 +18,9 @@ var (
|
|||||||
type Session struct {
|
type Session struct {
|
||||||
UserID uint64
|
UserID uint64
|
||||||
|
|
||||||
key string
|
key string
|
||||||
createdAt int64
|
createdAt int64
|
||||||
expiration *time.Timer
|
lock sync.Mutex
|
||||||
lock sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id uint64) (*Session, error) {
|
func New(id uint64) (*Session, error) {
|
||||||
@ -31,18 +30,12 @@ func New(id uint64) (*Session, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Session{
|
return &Session{
|
||||||
key: key,
|
key: key,
|
||||||
createdAt: time.Now().Unix(),
|
createdAt: time.Now().Unix(),
|
||||||
UserID: id,
|
UserID: id,
|
||||||
expiration: time.NewTimer(Expiration),
|
|
||||||
}, nil
|
}, 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 {
|
func (s *Session) Key() string {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
key := s.key
|
key := s.key
|
||||||
@ -51,11 +44,15 @@ func (s *Session) Key() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) SetCookie(w http.ResponseWriter, r *http.Request) {
|
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{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: CookieName,
|
Name: CookieName,
|
||||||
Value: s.Key(),
|
Value: s.Key(),
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Expires: time.Now().Add(Expiration),
|
Expires: created.Add(Expiration),
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: r.TLS != nil,
|
Secure: r.TLS != nil,
|
||||||
})
|
})
|
||||||
@ -83,8 +80,6 @@ func (s *Session) Refresh() (string, bool, error) {
|
|||||||
return "", false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.expiration.Reset(Expiration)
|
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
s.createdAt = time.Now().Unix()
|
s.createdAt = time.Now().Unix()
|
||||||
s.key = key
|
s.key = key
|
||||||
@ -95,10 +90,6 @@ func (s *Session) Refresh() (string, bool, error) {
|
|||||||
return "", false, nil
|
return "", false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) WaitUntilExpiration() {
|
|
||||||
<-s.expiration.C
|
|
||||||
}
|
|
||||||
|
|
||||||
func newSessionKey() (string, error) {
|
func newSessionKey() (string, error) {
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
_, err := rand.Read(key)
|
_, err := rand.Read(key)
|
||||||
|
@ -248,7 +248,6 @@ func newStateStore(sessionStore storage.SessionStore) *stateStore {
|
|||||||
|
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
if !session.Expired() {
|
if !session.Expired() {
|
||||||
session.Init()
|
|
||||||
store.sessions[session.Key()] = session
|
store.sessions[session.Key()] = session
|
||||||
} else {
|
} else {
|
||||||
go sessionStore.DeleteSession(session.Key())
|
go sessionStore.DeleteSession(session.Key())
|
||||||
|
Loading…
Reference in New Issue
Block a user