added batch limits

This commit is contained in:
Cam Otts 2023-01-12 14:09:04 -06:00
parent 0db3f0c9e6
commit 679aa09615
No known key found for this signature in database
GPG Key ID: 367B7C7EBD84A8BD
4 changed files with 29 additions and 20 deletions

View File

@ -68,12 +68,11 @@ type MaintenanceConfig struct {
type RegistrationMaintenanceConfig struct {
ExpirationTimeout time.Duration
CheckFrequency time.Duration
BatchLimit int
}
func LoadConfig(path string) (*Config, error) {
cfg := &Config{
Metrics: &MetricsConfig{ServiceName: "metrics"},
}
cfg := DefaultConfig()
if err := cf.BindYaml(cfg, path, cf.DefaultOptions()); err != nil {
return nil, errors.Wrapf(err, "error loading controller config '%v'", path)
}
@ -82,3 +81,16 @@ func LoadConfig(path string) (*Config, error) {
}
return cfg, nil
}
func DefaultConfig() *Config {
return &Config{
Metrics: &MetricsConfig{ServiceName: "metrics"},
Maintenance: &MaintenanceConfig{
Registration: &RegistrationMaintenanceConfig{
ExpirationTimeout: time.Hour * 24 * 30, //30 days
CheckFrequency: time.Hour,
BatchLimit: 500,
},
},
}
}

View File

@ -78,7 +78,7 @@ func Run(inCfg *Config) error {
}()
if cfg.Maintenance != nil && cfg.Maintenance.Registration != nil {
go newMaintenanceAgent(ctx, cfg.Maintenance.Registration.CheckFrequency, cfg.Maintenance.Registration.ExpirationTimeout).run()
go newMaintenanceAgent(ctx, cfg.Maintenance).run()
}
server := rest_server_zrok.NewServer(api)

View File

@ -11,21 +11,19 @@ import (
)
type maintenanceAgent struct {
ctx context.Context
frequency time.Duration
expiration time.Duration
*MaintenanceConfig
ctx context.Context
}
func newMaintenanceAgent(ctx context.Context, frequency, expiration time.Duration) *maintenanceAgent {
func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenanceAgent {
return &maintenanceAgent{
ctx: ctx,
frequency: frequency,
expiration: expiration,
MaintenanceConfig: cfg,
ctx: ctx,
}
}
func (ma *maintenanceAgent) run() {
ticker := time.NewTicker(ma.frequency)
ticker := time.NewTicker(ma.Registration.CheckFrequency)
for {
select {
case <-ma.ctx.Done():
@ -51,8 +49,8 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
}
defer func() { _ = tx.Rollback() }()
expir := time.Now().UTC().Add(-ma.expiration)
accountRequests, err := str.FindExpiredAccountRequests(expir, tx)
expir := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
accountRequests, err := str.FindExpiredAccountRequests(expir, ma.Registration.BatchLimit, tx)
if err != nil {
return errors.Wrapf(err, "error finding expire account requests before %v", expir)
}
@ -68,10 +66,9 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
if err := str.DeleteMultipleAccountRequests(ids, tx); err != nil {
return errors.Wrapf(err, "error deleting expired account requests before %v", expir)
}
}
if err := tx.Commit(); err != nil {
return errors.Wrapf(err, "error committing expired acount requests deletion")
if err := tx.Commit(); err != nil {
return errors.Wrapf(err, "error committing expired acount requests deletion")
}
}
return nil

View File

@ -44,8 +44,8 @@ func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*Acco
return ar, nil
}
func (self *Store) FindExpiredAccountRequests(before time.Time, tx *sqlx.Tx) ([]*AccountRequest, error) {
rows, err := tx.Queryx("select * from account_requests where created_at < $1", before)
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)
if err != nil {
return nil, errors.Wrap(err, "error selecting expired account_requests")
}