add configurable clear interval

This commit is contained in:
1e99 2024-10-30 00:25:53 +01:00
parent dfa37fc7fa
commit c8bbe24d4d
2 changed files with 12 additions and 10 deletions

View file

@ -6,11 +6,12 @@ import (
"time" "time"
) )
func NewRamStore() Store { func NewRamStore(clearInterval time.Duration) Store {
store := &ram{ store := &ram{
passwords: make(map[string]entry), clearInterval: clearInterval,
lock: sync.Mutex{}, passwords: make(map[string]entry),
close: make(chan bool), lock: sync.Mutex{},
close: make(chan bool),
} }
go store.clearExpired() go store.clearExpired()
@ -18,9 +19,10 @@ func NewRamStore() Store {
} }
type ram struct { type ram struct {
passwords map[string]entry clearInterval time.Duration
lock sync.Mutex passwords map[string]entry
close chan bool lock sync.Mutex
close chan bool
} }
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) { 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 { func (store *ram) clearExpired() error {
ticker := time.NewTicker(20 * time.Second) ticker := time.NewTicker(store.clearInterval)
defer ticker.Stop() defer ticker.Stop()
for { for {

View file

@ -30,10 +30,10 @@ func NewStore() (Store, error) {
switch storeType { switch storeType {
case "ram": case "ram":
return NewRamStore(), nil return NewRamStore(20 * time.Second), nil
default: default:
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.") log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
return NewRamStore(), nil return NewRamStore(20 * time.Second), nil
} }
} }