mirror of
https://github.com/openziti/zrok.git
synced 2024-12-22 23:02:52 +01:00
locking (#287)
This commit is contained in:
parent
2a770cc3b8
commit
af5afb8aac
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user