Skip to content

Commit

Permalink
Support garbage collection of expired tokens from cache
Browse files Browse the repository at this point in the history
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
welteki committed May 30, 2024
1 parent 07d4780 commit f1f79a5
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion tokencache.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package sdk

import "sync"
import (
"context"
"sync"
"time"
)

type TokenCache interface {
Get(key string) (*Token, bool)
Set(key string, token *Token)
}

// MemoryTokenCache is a basic in-memory token cache implementation.
type MemoryTokenCache struct {
tokens map[string]*Token

lock sync.RWMutex
}

// NewMemoryTokenCache creates a new in memory token cache instance.
func NewMemoryTokenCache() *MemoryTokenCache {
return &MemoryTokenCache{
tokens: map[string]*Token{},
}
}

// Set adds or updates a token with the given key in the cache.
func (c *MemoryTokenCache) Set(key string, token *Token) {
c.lock.Lock()
defer c.lock.Unlock()

c.tokens[key] = token
}

// Get retrieves the token associated with the given key from the cache. The bool
// return value will be false if no matching key is found, and true otherwise.
func (c *MemoryTokenCache) Get(key string) (*Token, bool) {
c.lock.RLock()
token, ok := c.tokens[key]
Expand All @@ -41,3 +50,33 @@ func (c *MemoryTokenCache) Get(key string) (*Token, bool) {

return token, ok
}

// StartGC starts garbage collection of expired tokens.
func (c *MemoryTokenCache) StartGC(ctx context.Context, gcInterval time.Duration) {
if gcInterval <= 0 {
return
}

ticker := time.NewTicker(gcInterval)

for {
select {
case <-ticker.C:
c.clearExpired()
case <-ctx.Done():
ticker.Stop()
return
}
}
}

// clearExpired removes all expired tokens from the cache.
func (c *MemoryTokenCache) clearExpired() {
for key, token := range c.tokens {
if token.Expired() {
c.lock.Lock()
delete(c.tokens, key)
c.lock.Unlock()
}
}
}

0 comments on commit f1f79a5

Please sign in to comment.