From c14ab3c44e64a0f1d6e156f62e3b1f5a4a686105 Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Wed, 12 Oct 2016 18:47:47 -0700 Subject: [PATCH] storage/memory: add garbage collection method --- storage/memory/memory.go | 42 ++++++++++++++++------------------- storage/memory/memory_test.go | 2 +- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/storage/memory/memory.go b/storage/memory/memory.go index b85d68df..df88b442 100644 --- a/storage/memory/memory.go +++ b/storage/memory/memory.go @@ -4,6 +4,7 @@ package memory import ( "strings" "sync" + "time" "github.com/coreos/dex/storage" ) @@ -51,6 +52,24 @@ func (s *memStorage) tx(f func()) { 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) { s.tx(func() { if _, ok := s.clients[c.ID]; ok { @@ -240,29 +259,6 @@ func (s *memStorage) GetAuthCode(id string) (c storage.AuthCode, err error) { 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) { s.tx(func() { client, ok := s.clients[id] diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go index 56c5f93d..faa76283 100644 --- a/storage/memory/memory_test.go +++ b/storage/memory/memory_test.go @@ -7,5 +7,5 @@ import ( ) func TestStorage(t *testing.T) { - conformance.RunTestSuite(t, New) + conformance.RunTests(t, New) }