add configurable clear interval
This commit is contained in:
parent
dfa37fc7fa
commit
c8bbe24d4d
2 changed files with 12 additions and 10 deletions
|
@ -6,11 +6,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func NewRamStore() Store {
|
||||
func NewRamStore(clearInterval time.Duration) Store {
|
||||
store := &ram{
|
||||
passwords: make(map[string]entry),
|
||||
lock: sync.Mutex{},
|
||||
close: make(chan bool),
|
||||
clearInterval: clearInterval,
|
||||
passwords: make(map[string]entry),
|
||||
lock: sync.Mutex{},
|
||||
close: make(chan bool),
|
||||
}
|
||||
|
||||
go store.clearExpired()
|
||||
|
@ -18,9 +19,10 @@ func NewRamStore() Store {
|
|||
}
|
||||
|
||||
type ram struct {
|
||||
passwords map[string]entry
|
||||
lock sync.Mutex
|
||||
close chan bool
|
||||
clearInterval time.Duration
|
||||
passwords map[string]entry
|
||||
lock sync.Mutex
|
||||
close chan bool
|
||||
}
|
||||
|
||||
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
|
||||
|
@ -64,7 +66,7 @@ func (store *ram) Close() error {
|
|||
}
|
||||
|
||||
func (store *ram) clearExpired() error {
|
||||
ticker := time.NewTicker(20 * time.Second)
|
||||
ticker := time.NewTicker(store.clearInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
|
|
|
@ -30,10 +30,10 @@ func NewStore() (Store, error) {
|
|||
|
||||
switch storeType {
|
||||
case "ram":
|
||||
return NewRamStore(), nil
|
||||
return NewRamStore(20 * time.Second), nil
|
||||
|
||||
default:
|
||||
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
|
||||
return NewRamStore(), nil
|
||||
return NewRamStore(20 * time.Second), nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue