added go routine to cleaup expired access requests

This commit is contained in:
Cam Otts 2023-01-12 10:04:56 -06:00
parent c4c497b88e
commit e69119896e
No known key found for this signature in database
GPG Key ID: 367B7C7EBD84A8BD
4 changed files with 100 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package controller
import (
"time"
"github.com/michaelquigley/cf"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/pkg/errors"
@ -18,6 +20,7 @@ type Config struct {
Ziti *ZitiConfig
Metrics *MetricsConfig
Influx *InfluxConfig
Maintenance *MaintenanceConfig
}
type AdminConfig struct {
@ -58,6 +61,15 @@ type InfluxConfig struct {
Token string `cf:"+secret"`
}
type MaintenanceConfig struct {
Registration *RegistrationMaintenanceConfig
}
type RegistrationMaintenanceConfig struct {
ExpirationTimeout time.Duration
CheckFrequency time.Duration
}
func LoadConfig(path string) (*Config, error) {
cfg := &Config{
Metrics: &MetricsConfig{ServiceName: "metrics"},

View File

@ -1,6 +1,8 @@
package controller
import (
"context"
"github.com/go-openapi/loads"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/openziti-test-kitchen/zrok/controller/store"
@ -70,6 +72,15 @@ func Run(inCfg *Config) error {
}()
}
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
}()
if cfg.Maintenance != nil && cfg.Maintenance.Registration != nil {
go newMaintenanceAgent(ctx, cfg.Maintenance.Registration.CheckFrequency, cfg.Maintenance.Registration.ExpirationTimeout).run()
}
server := rest_server_zrok.NewServer(api)
defer func() { _ = server.Shutdown() }()
server.Host = cfg.Endpoint.Host

61
controller/maintenance.go Normal file
View File

@ -0,0 +1,61 @@
package controller
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type maintenanceAgent struct {
ctx context.Context
frequency time.Duration
expiration time.Duration
}
func newMaintenanceAgent(ctx context.Context, frequency, expiration time.Duration) *maintenanceAgent {
return &maintenanceAgent{
ctx: ctx,
frequency: frequency,
expiration: expiration,
}
}
func (ma *maintenanceAgent) run() {
ticker := time.NewTicker(ma.frequency)
for {
select {
case <-ma.ctx.Done():
{
ticker.Stop()
return
}
case <-ticker.C:
{
logrus.Info("TICK")
if err := ma.deleteExpiredAccountRequests(); err != nil {
logrus.Error(err)
}
}
}
}
}
func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
tx, err := str.Begin()
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
if err := str.DeleteExpiredAccountRequests(time.Now().UTC().Add(-ma.expiration), tx); err != nil {
return errors.Wrapf(err, "error deleting expired account requests")
}
if err := tx.Commit(); err != nil {
return errors.Wrapf(err, "error committing expired acount requests deletion")
}
return nil
}

View File

@ -1,8 +1,11 @@
package store
import (
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type AccountRequest struct {
@ -59,3 +62,16 @@ func (self *Store) DeleteAccountRequest(id int, tx *sqlx.Tx) error {
}
return nil
}
func (self *Store) DeleteExpiredAccountRequests(before time.Time, tx *sqlx.Tx) error {
stmt, err := tx.Prepare("delete from account_requests where created_at < $1")
logrus.Infof("Trying to delete account requests older than %v", before)
if err != nil {
return errors.Wrap(err, "error preparing account_requests delete expired statement")
}
_, err = stmt.Exec(before)
if err != nil {
return errors.Wrap(err, "error executing account_requests delete expired statement")
}
return nil
}