2023-09-26 20:14:57 +02:00
|
|
|
package memstore
|
2017-08-07 05:04:35 +02:00
|
|
|
|
|
|
|
import (
|
2020-08-12 06:48:45 +02:00
|
|
|
"sync"
|
2017-08-07 05:04:35 +02:00
|
|
|
"time"
|
2023-09-26 20:14:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Clock is the default clock to get the current time,
|
|
|
|
// it can be used for testing purposes too.
|
|
|
|
//
|
|
|
|
// Defaults to time.Now.
|
|
|
|
Clock func() time.Time = time.Now
|
2020-05-09 13:04:51 +02:00
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
// ExpireDelete may be set on Cookie.Expire for expiring the given cookie.
|
2023-12-25 22:55:31 +01:00
|
|
|
ExpireDelete = time.Unix(0, 0) // time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
2017-08-07 05:04:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// LifeTime controls the session expiration datetime.
|
|
|
|
type LifeTime struct {
|
|
|
|
// Remember, tip for the future:
|
|
|
|
// No need of gob.Register, because we embed the time.Time.
|
|
|
|
// And serious bug which has a result of me spending my whole evening:
|
|
|
|
// Because of gob encoding it doesn't encodes/decodes the other fields if time.Time is embedded
|
|
|
|
// (this should be a bug(go1.9-rc1) or not. We don't care atm)
|
|
|
|
time.Time
|
|
|
|
timer *time.Timer
|
2020-08-12 06:48:45 +02:00
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
// StartTime holds the Now of the Begin.
|
|
|
|
Begun time.Time
|
|
|
|
|
2020-08-12 06:48:45 +02:00
|
|
|
mu sync.RWMutex
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
// NewLifeTime returns a pointer to an empty LifeTime instance.
|
|
|
|
func NewLifeTime() *LifeTime {
|
|
|
|
return &LifeTime{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin will begin the life based on the Clock (time.Now()).Add(d).
|
2017-08-07 05:04:35 +02:00
|
|
|
// Use `Continue` to continue from a stored time(database-based session does that).
|
|
|
|
func (lt *LifeTime) Begin(d time.Duration, onExpire func()) {
|
|
|
|
if d <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
now := Clock()
|
|
|
|
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Lock()
|
2023-09-26 20:14:57 +02:00
|
|
|
lt.Begun = now
|
|
|
|
lt.Time = now.Add(d)
|
2017-08-07 05:04:35 +02:00
|
|
|
lt.timer = time.AfterFunc(d, onExpire)
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Unlock()
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revive will continue the life based on the stored Time.
|
|
|
|
// Other words that could be used for this func are: Continue, Restore, Resc.
|
|
|
|
func (lt *LifeTime) Revive(onExpire func()) {
|
2023-09-26 20:14:57 +02:00
|
|
|
lt.mu.RLock()
|
|
|
|
t := lt.Time
|
|
|
|
lt.mu.RUnlock()
|
|
|
|
|
|
|
|
if t.IsZero() {
|
2017-08-07 05:04:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
now := Clock()
|
|
|
|
if t.After(now) {
|
|
|
|
d := t.Sub(now)
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Lock()
|
2023-09-26 20:14:57 +02:00
|
|
|
if lt.timer != nil {
|
|
|
|
lt.timer.Stop() // Stop the existing timer, if any.
|
|
|
|
}
|
|
|
|
lt.Begun = now
|
|
|
|
lt.timer = time.AfterFunc(d, onExpire) // and execute on-time the new onExpire function.
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Unlock()
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift resets the lifetime based on "d".
|
|
|
|
func (lt *LifeTime) Shift(d time.Duration) {
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Lock()
|
2017-08-07 05:04:35 +02:00
|
|
|
if d > 0 && lt.timer != nil {
|
2023-09-26 20:14:57 +02:00
|
|
|
now := Clock()
|
|
|
|
lt.Begun = now
|
|
|
|
lt.Time = now.Add(d)
|
2017-08-07 05:04:35 +02:00
|
|
|
lt.timer.Reset(d)
|
|
|
|
}
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Unlock()
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExpireNow reduce the lifetime completely.
|
|
|
|
func (lt *LifeTime) ExpireNow() {
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Lock()
|
2023-09-26 20:14:57 +02:00
|
|
|
lt.Time = ExpireDelete
|
2017-08-07 05:04:35 +02:00
|
|
|
if lt.timer != nil {
|
|
|
|
lt.timer.Stop()
|
|
|
|
}
|
2020-08-12 06:48:45 +02:00
|
|
|
lt.mu.Unlock()
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasExpired reports whether "lt" represents is expired.
|
|
|
|
func (lt *LifeTime) HasExpired() bool {
|
2023-09-26 20:14:57 +02:00
|
|
|
lt.mu.RLock()
|
|
|
|
t := lt.Time
|
|
|
|
lt.mu.RUnlock()
|
|
|
|
|
|
|
|
if t.IsZero() {
|
2017-08-07 05:04:35 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:14:57 +02:00
|
|
|
return t.Before(Clock())
|
2017-08-07 05:04:35 +02:00
|
|
|
}
|
2018-04-22 12:52:36 +02:00
|
|
|
|
|
|
|
// DurationUntilExpiration returns the duration until expires, it can return negative number if expired,
|
|
|
|
// a call to `HasExpired` may be useful before calling this `Dur` function.
|
|
|
|
func (lt *LifeTime) DurationUntilExpiration() time.Duration {
|
2023-09-26 20:14:57 +02:00
|
|
|
lt.mu.RLock()
|
|
|
|
t := lt.Time
|
|
|
|
lt.mu.RUnlock()
|
|
|
|
|
|
|
|
return t.Sub(Clock())
|
2018-04-22 12:52:36 +02:00
|
|
|
}
|