Skip to content

Commit

Permalink
cookie: implement garbage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Nov 18, 2023
1 parent 8a0f5eb commit 8437ab6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions contrib/sample-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cookie:
# ## generate with `openssl pkey -in ./contrib/bar_ed25519_priv.pem -pubout -out ./contrib/bar_ed25519_pub.pem`
# public-key-file: ./contrib/bar_ed25519_pub.pem
backend:
# gc-interval: 10s
in-memory: {}

auth:
Expand Down
6 changes: 4 additions & 2 deletions cookie/backend_in-memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ func (b *InMemoryBackend) ListRevoked() (list StoredSessionList, err error) {
return
}

func (b *InMemoryBackend) CollectGarbage() error {
func (b *InMemoryBackend) CollectGarbage() (uint, error) {
b.mutex.RLock()
defer b.mutex.RUnlock()

cnt := uint(0)
for _, sessions := range b.sessions {
for id, session := range sessions {
if session.IsExpired() {
delete(sessions, id)
cnt = cnt + 1
}
}
}
Expand All @@ -138,5 +140,5 @@ func (b *InMemoryBackend) CollectGarbage() error {
}
}

return nil
return cnt, nil
}
38 changes: 34 additions & 4 deletions cookie/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type SignerVerifierConfig struct {
}

type StoreBackendConfig struct {
InMemory *InMemoryBackendConfig `yaml:"in-memory"`
GCInterval time.Duration `yaml:"gc-interval"`
InMemory *InMemoryBackendConfig `yaml:"in-memory"`
}

type Config struct {
Expand Down Expand Up @@ -96,7 +97,7 @@ type StoreBackend interface {
Revoke(username string, id ulid.ULID) error
IsRevoked(id ulid.ULID) (bool, error)
ListRevoked() (StoredSessionList, error)
CollectGarbage() error
CollectGarbage() (uint, error)
}

type Options struct {
Expand Down Expand Up @@ -180,13 +181,42 @@ func (st *Store) initKeys(conf *Config) (err error) {
return
}

func (st *Store) runGC(interval time.Duration) {
t := time.NewTicker(interval)
st.dbgLog.Printf("cookie-store: running GC every %v", interval)
for {
if _, ok := <-t.C; !ok {
return
}
cnt, err := st.backend.CollectGarbage()
if err != nil {
st.infoLog.Printf("cookie-store: failed to collect garbage: %v", err)
}
if cnt > 0 {
st.dbgLog.Printf("cookie-store: GC removed %d expired sessions", cnt)
}
}
}

func (st *Store) initBackend(conf *Config) (err error) {
if conf.Backend.GCInterval <= time.Second {
st.infoLog.Printf("cookie-store: overriding invalid/unset GC interval to 5 minutes")
conf.Backend.GCInterval = 5 * time.Minute
}

if conf.Backend.InMemory != nil {
st.backend, err = NewInMemoryBackend(conf.Backend.InMemory)
if err != nil {
return err
}
}
// TODO: add other backend types
if st.backend == nil {
err = fmt.Errorf("no valid backend configuration found")
return
}
// TODO: add garbage collector!!
err = fmt.Errorf("no valid backend configuration found")

go st.runGC(conf.Backend.GCInterval)
return
}

Expand Down

0 comments on commit 8437ab6

Please sign in to comment.