Commit c14ab3c4 authored by Eric Chiang's avatar Eric Chiang

storage/memory: add garbage collection method

parent d27f5e41
...@@ -4,6 +4,7 @@ package memory ...@@ -4,6 +4,7 @@ package memory
import ( import (
"strings" "strings"
"sync" "sync"
"time"
"github.com/coreos/dex/storage" "github.com/coreos/dex/storage"
) )
...@@ -51,6 +52,24 @@ func (s *memStorage) tx(f func()) { ...@@ -51,6 +52,24 @@ func (s *memStorage) tx(f func()) {
func (s *memStorage) Close() error { return nil } func (s *memStorage) Close() error { return nil }
func (s *memStorage) GarbageCollect(now time.Time) (result storage.GCResult, err error) {
s.tx(func() {
for id, a := range s.authCodes {
if now.After(a.Expiry) {
delete(s.authCodes, id)
result.AuthCodes++
}
}
for id, a := range s.authReqs {
if now.After(a.Expiry) {
delete(s.authReqs, id)
result.AuthRequests++
}
}
})
return result, nil
}
func (s *memStorage) CreateClient(c storage.Client) (err error) { func (s *memStorage) CreateClient(c storage.Client) (err error) {
s.tx(func() { s.tx(func() {
if _, ok := s.clients[c.ID]; ok { if _, ok := s.clients[c.ID]; ok {
...@@ -240,29 +259,6 @@ func (s *memStorage) GetAuthCode(id string) (c storage.AuthCode, err error) { ...@@ -240,29 +259,6 @@ func (s *memStorage) GetAuthCode(id string) (c storage.AuthCode, err error) {
return return
} }
func (s *memStorage) ClaimCode(id string) (err error) {
s.tx(func() {
if _, ok := s.authCodes[id]; !ok {
err = storage.ErrNotFound
return
}
delete(s.authCodes, id)
})
return
}
func (s *memStorage) ClaimRefresh(refreshToken string) (token storage.RefreshToken, err error) {
s.tx(func() {
var ok bool
if token, ok = s.refreshTokens[refreshToken]; !ok {
err = storage.ErrNotFound
return
}
delete(s.refreshTokens, refreshToken)
})
return
}
func (s *memStorage) UpdateClient(id string, updater func(old storage.Client) (storage.Client, error)) (err error) { func (s *memStorage) UpdateClient(id string, updater func(old storage.Client) (storage.Client, error)) (err error) {
s.tx(func() { s.tx(func() {
client, ok := s.clients[id] client, ok := s.clients[id]
......
...@@ -7,5 +7,5 @@ import ( ...@@ -7,5 +7,5 @@ import (
) )
func TestStorage(t *testing.T) { func TestStorage(t *testing.T) {
conformance.RunTestSuite(t, New) conformance.RunTests(t, New)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment