diff --git a/controller/limits/agent.go b/controller/limits/agent.go index 97455618..9dd9332e 100644 --- a/controller/limits/agent.go +++ b/controller/limits/agent.go @@ -66,6 +66,9 @@ func (a *Agent) Stop() { func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) { if a.cfg.Enforcing { + if err := a.str.LimitCheckLock(acctId, trx); err != nil { + return false, err + } if empty, err := a.str.IsAccountLimitJournalEmpty(acctId, trx); err == nil && !empty { alj, err := a.str.FindLatestAccountLimitJournal(acctId, trx) if err != nil { @@ -93,6 +96,9 @@ func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) { func (a *Agent) CanCreateShare(acctId, envId int, trx *sqlx.Tx) (bool, error) { if a.cfg.Enforcing { + if err := a.str.LimitCheckLock(acctId, trx); err != nil { + return false, err + } if empty, err := a.str.IsAccountLimitJournalEmpty(acctId, trx); err == nil && !empty { alj, err := a.str.FindLatestAccountLimitJournal(acctId, trx) if err != nil { diff --git a/controller/store/limitCheckLock.go b/controller/store/limitCheckLock.go index 59ad1b1a..edbae33b 100644 --- a/controller/store/limitCheckLock.go +++ b/controller/store/limitCheckLock.go @@ -6,12 +6,14 @@ import ( ) func (str *Store) LimitCheckLock(acctId int, trx *sqlx.Tx) error { - stmt, err := trx.Prepare("insert into limit_check_locks (account_id) values ($1) on conflict (account_id) do update set updated_at = current_timestamp") - if err != nil { - return errors.Wrap(err, "error preparing upsert on limit_check_locks") - } - if _, err := stmt.Exec(acctId); err != nil { - return errors.Wrap(err, "error executing upsert on limit_check_locks") + if str.cfg.EnableLocking { + stmt, err := trx.Prepare("insert into limit_check_locks (account_id) values ($1) on conflict (account_id) do update set updated_at = current_timestamp") + if err != nil { + return errors.Wrap(err, "error preparing upsert on limit_check_locks") + } + if _, err := stmt.Exec(acctId); err != nil { + return errors.Wrap(err, "error executing upsert on limit_check_locks") + } } return nil }