account expiration tweaks (#135)

This commit is contained in:
Michael Quigley 2023-01-12 16:00:09 -05:00
parent 9491e13307
commit 2da67d4a29
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 24 additions and 9 deletions

View File

@ -1,10 +1,10 @@
package controller
import (
"time"
"github.com/michaelquigley/cf"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/pkg/errors"
"time"
)
const ConfigVersion = 1
@ -76,7 +76,7 @@ func DefaultConfig() *Config {
Metrics: &MetricsConfig{ServiceName: "metrics"},
Maintenance: &MaintenanceConfig{
Registration: &RegistrationMaintenanceConfig{
ExpirationTimeout: time.Hour * 24 * 30, //30 days
ExpirationTimeout: time.Hour * 24,
CheckFrequency: time.Hour,
BatchLimit: 500,
},

View File

@ -23,12 +23,14 @@ func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenan
}
func (ma *maintenanceAgent) run() {
logrus.Info("starting")
defer logrus.Info("stopping")
ticker := time.NewTicker(ma.Registration.CheckFrequency)
for {
select {
case <-ma.ctx.Done():
{
logrus.Info("stopping maintenance loop...")
ticker.Stop()
return
}
@ -49,22 +51,23 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
}
defer func() { _ = tx.Rollback() }()
expir := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
accountRequests, err := str.FindExpiredAccountRequests(expir, ma.Registration.BatchLimit, tx)
timeout := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
accountRequests, err := str.FindExpiredAccountRequests(timeout, ma.Registration.BatchLimit, tx)
if err != nil {
return errors.Wrapf(err, "error finding expire account requests before %v", expir)
return errors.Wrapf(err, "error finding expire account requests before %v", timeout)
}
if len(accountRequests) > 0 {
logrus.Infof("found %d expired account requests to remove", len(accountRequests))
acctStrings := make([]string, len(accountRequests))
ids := make([]int, len(accountRequests))
for i, acct := range accountRequests {
ids[i] = acct.Id
acctStrings[i] = fmt.Sprintf("{%d:%s}", acct.Id, acct.Email)
}
logrus.Infof("starting deleting for expired account requests: %v", strings.Join(acctStrings, ","))
logrus.Infof("deleting expired account requests: %v", strings.Join(acctStrings, ","))
if err := str.DeleteMultipleAccountRequests(ids, tx); err != nil {
return errors.Wrapf(err, "error deleting expired account requests before %v", expir)
return errors.Wrapf(err, "error deleting expired account requests before %v", timeout)
}
if err := tx.Commit(); err != nil {
return errors.Wrapf(err, "error committing expired acount requests deletion")

View File

@ -45,7 +45,19 @@ func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*Acco
}
func (self *Store) FindExpiredAccountRequests(before time.Time, limit int, tx *sqlx.Tx) ([]*AccountRequest, error) {
rows, err := tx.Queryx(fmt.Sprintf("select * from account_requests where created_at < $1 limit %d for update", limit), before)
var sql string
switch self.cfg.Type {
case "postgres":
sql = "select * from account_requests where created_at < $1 limit %d for update"
case "sqlite3":
sql = "select * from account_requests where created_at < $1 limit %d"
default:
return nil, errors.Errorf("unknown database type '%v'", self.cfg.Type)
}
rows, err := tx.Queryx(fmt.Sprintf(sql, limit), before)
if err != nil {
return nil, errors.Wrap(err, "error selecting expired account_requests")
}