This commit is contained in:
Michael Quigley 2024-05-15 15:48:42 -04:00
parent 2a770cc3b8
commit af5afb8aac
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 14 additions and 6 deletions

View File

@ -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 {

View File

@ -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
}