From c8bbe24d4d9571376cd677cec8323c4d62a68074 Mon Sep 17 00:00:00 2001 From: 1e99 <1e99@1e99.eu> Date: Wed, 30 Oct 2024 00:25:53 +0100 Subject: [PATCH] add configurable clear interval --- storage/ram.go | 18 ++++++++++-------- storage/storage.go | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/storage/ram.go b/storage/ram.go index 6ff4dc9..87ba129 100644 --- a/storage/ram.go +++ b/storage/ram.go @@ -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 { diff --git a/storage/storage.go b/storage/storage.go index 8151500..3d7dac1 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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 } }