scoped classes now include resource limits (#606)

This commit is contained in:
Michael Quigley 2024-06-07 15:05:18 -04:00
parent 60893100a5
commit a389674d51
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 14 additions and 10 deletions

View File

@ -107,7 +107,11 @@ func (a *Agent) CanCreateShare(acctId, envId int, reserved, uniqueName bool, _ s
}
}
if ul.resource.GetShares() > store.Unlimited || (reserved && ul.resource.GetReservedShares() > store.Unlimited) || (reserved && uniqueName && ul.resource.GetUniqueNames() > store.Unlimited) {
rc := ul.resource
if scopeRc, found := ul.scopes[backendMode]; found {
rc = scopeRc
}
if rc.GetShares() > store.Unlimited || (reserved && rc.GetReservedShares() > store.Unlimited) || (reserved && uniqueName && rc.GetUniqueNames() > store.Unlimited) {
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
if err != nil {
return false, err
@ -129,15 +133,15 @@ func (a *Agent) CanCreateShare(acctId, envId int, reserved, uniqueName bool, _ s
uniqueNames++
}
}
if total+1 > ul.resource.GetShares() {
if total+1 > rc.GetShares() {
logrus.Debugf("account '%d', environment '%d' over shares limit '%d'", acctId, envId, a.cfg.Shares)
return false, nil
}
if reserved && reserveds+1 > ul.resource.GetReservedShares() {
if reserved && reserveds+1 > rc.GetReservedShares() {
logrus.Debugf("account '%v', environment '%d' over reserved shares limit '%d'", acctId, envId, a.cfg.ReservedShares)
return false, nil
}
if reserved && uniqueName && uniqueNames+1 > ul.resource.GetUniqueNames() {
if reserved && uniqueName && uniqueNames+1 > rc.GetUniqueNames() {
logrus.Debugf("account '%v', environment '%d' over unique names limit '%d'", acctId, envId, a.cfg.UniqueNames)
return false, nil
}

View File

@ -11,7 +11,7 @@ import (
type userLimits struct {
resource store.ResourceCountClass
bandwidth []store.BandwidthClass
scopes map[sdk.BackendMode]store.BandwidthClass
scopes map[sdk.BackendMode]*store.LimitClass
}
func (ul *userLimits) toBandwidthArray(backendMode sdk.BackendMode) []store.BandwidthClass {
@ -50,7 +50,7 @@ func (a *Agent) getUserLimits(acctId int, trx *sqlx.Tx) (*userLimits, error) {
cfgBwcs := newConfigBandwidthClasses(a.cfg.Bandwidth)
bwWarning := cfgBwcs[0]
bwLimit := cfgBwcs[1]
scopes := map[sdk.BackendMode]store.BandwidthClass{}
scopes := make(map[sdk.BackendMode]*store.LimitClass)
alcs, err := a.str.FindAppliedLimitClassesForAccount(acctId, trx)
if err != nil {
@ -65,10 +65,10 @@ func (a *Agent) getUserLimits(acctId int, trx *sqlx.Tx) (*userLimits, error) {
} else {
bwLimit = alc
}
} else if a.isScopedBandwidthClass(alc) {
} else if a.isScopedLimitClass(alc) {
scopes[*alc.BackendMode] = alc
} else {
logrus.Warnf("unknown type of limit class '%v'", alc)
logrus.Warnf("unknown type of limit class '%v' for account '#%d'", alc, acctId)
}
}
@ -107,11 +107,11 @@ func (a *Agent) isUnscopedBandwidthClass(alc *store.LimitClass) bool {
return true
}
func (a *Agent) isScopedBandwidthClass(alc *store.LimitClass) bool {
func (a *Agent) isScopedLimitClass(alc *store.LimitClass) bool {
if alc.BackendMode == nil {
return false
}
if alc.Environments > store.Unlimited || alc.Shares > store.Unlimited || alc.ReservedShares > store.Unlimited || alc.UniqueNames > store.Unlimited {
if alc.Environments > store.Unlimited {
return false
}
if alc.PeriodMinutes < 1 {