Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
purge expired tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Feb 19, 2020
1 parent 8b14d53 commit a2b29db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
ResponseBufferCap = 100
TokenLength = 12
TokenValidityDays = 7
TokenPurgeInterval = time.Hour
)

type event struct {
Expand All @@ -53,6 +54,7 @@ func startManager(pipe model.Pipe, zmqConf model.ZeromqServerInfo, storageClient
return nil, fmt.Errorf("error creating keys for CA: %s", err)
}

go m.purgeExpiredTokens()
go m.manageResponses()
return m, nil
}
Expand Down Expand Up @@ -432,6 +434,19 @@ func (m *manager) getServerInfo() (*model.ServerInfo, error) {
}, nil
}

func (m *manager) purgeExpiredTokens() {
for t := time.Now(); true; t = <-time.Tick(TokenPurgeInterval) {
total, err := m.storage.PurgeOldTokens(t.AddDate(0, 0, -TokenValidityDays))
if err != nil {
log.Printf("Error purging tokens: %s", err)
continue
}
if total > 0 {
log.Printf("Purged %d old tokens.", total)
}
}
}

type tokenSet struct {
Name string `json:"name"`
Available int `json:"available"`
Expand Down
11 changes: 11 additions & 0 deletions manager/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Storage interface {
findToken(hash string) (found bool, err error)
DeleteTokenTrans(hash string) (found bool, trans *transaction, err error)
DeleteTokens(name string) error
PurgeOldTokens(to time.Time) (total int64, err error)
//
DoBulk(...interface{}) error
}
Expand Down Expand Up @@ -734,6 +735,16 @@ func (s *storage) DeleteTokens(name string) error {
return nil
}

func (s *storage) PurgeOldTokens(to time.Time) (int64, error) {
query := elastic.NewBoolQuery().Filter(elastic.NewRangeQuery("expiresAt").To(to))

res, err := s.client.DeleteByQuery(indexToken).Query(query).Do(s.ctx)
if err != nil {
return 0, err
}
return res.Total, nil
}

// DoBulk performs elastic.BulkableRequests
func (s *storage) DoBulk(requests ...interface{}) error {
bulk := s.client.Bulk()
Expand Down
Empty file added tests/README.md
Empty file.

0 comments on commit a2b29db

Please sign in to comment.