mirror of
https://github.com/openziti/zrok.git
synced 2024-12-22 06:40:50 +01:00
added explicit get and delete for expired account requests
This commit is contained in:
parent
deb6831fee
commit
0db3f0c9e6
@ -2,6 +2,8 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@ -28,7 +30,7 @@ func (ma *maintenanceAgent) run() {
|
||||
select {
|
||||
case <-ma.ctx.Done():
|
||||
{
|
||||
logrus.Info("Stopping maintenance loop...")
|
||||
logrus.Info("stopping maintenance loop...")
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
@ -49,8 +51,23 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
|
||||
}
|
||||
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")
|
||||
expir := time.Now().UTC().Add(-ma.expiration)
|
||||
accountRequests, err := str.FindExpiredAccountRequests(expir, tx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error finding expire account requests before %v", expir)
|
||||
}
|
||||
if len(accountRequests) > 0 {
|
||||
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, ","))
|
||||
|
||||
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 {
|
||||
|
@ -1,11 +1,12 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AccountRequest struct {
|
||||
@ -43,6 +44,22 @@ 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)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error selecting expired account_requests")
|
||||
}
|
||||
var ars []*AccountRequest
|
||||
for rows.Next() {
|
||||
ar := &AccountRequest{}
|
||||
if err := rows.StructScan(ar); err != nil {
|
||||
return nil, errors.Wrap(err, "error scanning account_request")
|
||||
}
|
||||
ars = append(ars, ar)
|
||||
}
|
||||
return ars, nil
|
||||
}
|
||||
|
||||
func (self *Store) FindAccountRequestWithEmail(email string, tx *sqlx.Tx) (*AccountRequest, error) {
|
||||
ar := &AccountRequest{}
|
||||
if err := tx.QueryRowx("select * from account_requests where email = $1", email).StructScan(ar); err != nil {
|
||||
@ -63,15 +80,26 @@ 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")
|
||||
func (self *Store) DeleteMultipleAccountRequests(ids []int, tx *sqlx.Tx) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err = stmt.Exec(before)
|
||||
|
||||
anyIds := make([]any, len(ids))
|
||||
indexes := make([]string, len(ids))
|
||||
|
||||
for i, id := range ids {
|
||||
anyIds[i] = id
|
||||
indexes[i] = fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(fmt.Sprintf("delete from account_requests where id in (%s)", strings.Join(indexes, ",")))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing account_requests delete expired statement")
|
||||
return errors.Wrap(err, "error preparing account_requests delete multiple statement")
|
||||
}
|
||||
_, err = stmt.Exec(anyIds...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing account_requests delete multiple statement")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user