mirror of
https://github.com/openziti/zrok.git
synced 2025-02-18 11:10:57 +01:00
account expiration tweaks (#135)
This commit is contained in:
parent
9491e13307
commit
2da67d4a29
@ -1,10 +1,10 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
"github.com/michaelquigley/cf"
|
"github.com/michaelquigley/cf"
|
||||||
"github.com/openziti-test-kitchen/zrok/controller/store"
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ConfigVersion = 1
|
const ConfigVersion = 1
|
||||||
@ -76,7 +76,7 @@ func DefaultConfig() *Config {
|
|||||||
Metrics: &MetricsConfig{ServiceName: "metrics"},
|
Metrics: &MetricsConfig{ServiceName: "metrics"},
|
||||||
Maintenance: &MaintenanceConfig{
|
Maintenance: &MaintenanceConfig{
|
||||||
Registration: &RegistrationMaintenanceConfig{
|
Registration: &RegistrationMaintenanceConfig{
|
||||||
ExpirationTimeout: time.Hour * 24 * 30, //30 days
|
ExpirationTimeout: time.Hour * 24,
|
||||||
CheckFrequency: time.Hour,
|
CheckFrequency: time.Hour,
|
||||||
BatchLimit: 500,
|
BatchLimit: 500,
|
||||||
},
|
},
|
||||||
|
@ -23,12 +23,14 @@ func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenan
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ma *maintenanceAgent) run() {
|
func (ma *maintenanceAgent) run() {
|
||||||
|
logrus.Info("starting")
|
||||||
|
defer logrus.Info("stopping")
|
||||||
|
|
||||||
ticker := time.NewTicker(ma.Registration.CheckFrequency)
|
ticker := time.NewTicker(ma.Registration.CheckFrequency)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ma.ctx.Done():
|
case <-ma.ctx.Done():
|
||||||
{
|
{
|
||||||
logrus.Info("stopping maintenance loop...")
|
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -49,22 +51,23 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
|
|||||||
}
|
}
|
||||||
defer func() { _ = tx.Rollback() }()
|
defer func() { _ = tx.Rollback() }()
|
||||||
|
|
||||||
expir := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
|
timeout := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
|
||||||
accountRequests, err := str.FindExpiredAccountRequests(expir, ma.Registration.BatchLimit, tx)
|
accountRequests, err := str.FindExpiredAccountRequests(timeout, ma.Registration.BatchLimit, tx)
|
||||||
if err != nil {
|
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 {
|
if len(accountRequests) > 0 {
|
||||||
|
logrus.Infof("found %d expired account requests to remove", len(accountRequests))
|
||||||
acctStrings := make([]string, len(accountRequests))
|
acctStrings := make([]string, len(accountRequests))
|
||||||
ids := make([]int, len(accountRequests))
|
ids := make([]int, len(accountRequests))
|
||||||
for i, acct := range accountRequests {
|
for i, acct := range accountRequests {
|
||||||
ids[i] = acct.Id
|
ids[i] = acct.Id
|
||||||
acctStrings[i] = fmt.Sprintf("{%d:%s}", acct.Id, acct.Email)
|
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 {
|
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 {
|
if err := tx.Commit(); err != nil {
|
||||||
return errors.Wrapf(err, "error committing expired acount requests deletion")
|
return errors.Wrapf(err, "error committing expired acount requests deletion")
|
||||||
|
@ -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) {
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error selecting expired account_requests")
|
return nil, errors.Wrap(err, "error selecting expired account_requests")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user