storage/memory: add garbage collection method

This commit is contained in:
Eric Chiang 2016-10-12 18:47:47 -07:00
parent d27f5e411f
commit c14ab3c44e
2 changed files with 20 additions and 24 deletions

View file

@ -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()) {
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) {
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]

View file

@ -7,5 +7,5 @@ import (
) )
func TestStorage(t *testing.T) { func TestStorage(t *testing.T) {
conformance.RunTestSuite(t, New) conformance.RunTests(t, New)
} }