frontends -> share_frontends; CanAccessShare refactoring (#650)

This commit is contained in:
Michael Quigley 2024-06-17 12:12:38 -04:00
parent f260449604
commit f174abd18f
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
6 changed files with 37 additions and 26 deletions

View File

@ -163,32 +163,43 @@ func (a *Agent) CanAccessShare(shrId int, trx *sqlx.Tx) (bool, error) {
return false, err return false, err
} }
if env.AccountId != nil { if env.AccountId != nil {
if err := a.str.LimitCheckLock(*env.AccountId, trx); err != nil {
return false, err
}
ul, err := a.getUserLimits(*env.AccountId, trx) ul, err := a.getUserLimits(*env.AccountId, trx)
if err != nil { if err != nil {
return false, err return false, err
} }
if ul.resource.IsGlobal() { if scopedBwc, found := ul.scopes[sdk.BackendMode(shr.BackendMode)]; found {
if empty, err := a.str.IsBandwidthLimitJournalEmptyForGlobal(*env.AccountId, trx); err == nil && !empty { latestScopedJe, err := a.isBandwidthClassLimitedForAccount(*env.AccountId, scopedBwc, trx)
lj, err := a.str.FindLatestBandwidthLimitJournalForGlobal(*env.AccountId, trx) if err != nil {
if err != nil { return false, err
return false, err }
} if latestScopedJe != nil {
if lj.Action == store.LimitLimitAction { return false, nil
return false, nil
}
} }
} else { } else {
if empty, err := a.str.IsBandwidthLimitJournalEmptyForLimitClass(*env.AccountId, ul.resource.GetLimitClassId(), trx); err == nil && !empty { for _, bwc := range ul.bandwidth {
lj, err := a.str.FindLatestBandwidthLimitJournalForLimitClass(*env.AccountId, ul.resource.GetLimitClassId(), trx) latestJe, err := a.isBandwidthClassLimitedForAccount(*env.AccountId, bwc, trx)
if err != nil { if err != nil {
return false, err return false, err
} }
if lj.Action == store.LimitLimitAction { if latestJe != nil {
return false, nil return false, nil
} }
} }
} }
rc := ul.resource
if scopeRc, found := ul.scopes[sdk.BackendMode(shr.BackendMode)]; found {
rc = scopeRc
}
if rc.GetShareFrontends() > store.Unlimited {
// TODO: Implement frontends+1 check
return true, nil
}
} else { } else {
return false, nil return false, nil
} }

View File

@ -10,7 +10,7 @@ type Config struct {
Shares int Shares int
ReservedShares int ReservedShares int
UniqueNames int UniqueNames int
Frontends int ShareFrontends int
Bandwidth *BandwidthPerPeriod Bandwidth *BandwidthPerPeriod
Cycle time.Duration Cycle time.Duration
Enforcing bool Enforcing bool
@ -50,7 +50,7 @@ func DefaultConfig() *Config {
Shares: store.Unlimited, Shares: store.Unlimited,
ReservedShares: store.Unlimited, ReservedShares: store.Unlimited,
UniqueNames: store.Unlimited, UniqueNames: store.Unlimited,
Frontends: store.Unlimited, ShareFrontends: store.Unlimited,
Bandwidth: DefaultBandwidthPerPeriod(), Bandwidth: DefaultBandwidthPerPeriod(),
Enforcing: false, Enforcing: false,
Cycle: 15 * time.Minute, Cycle: 15 * time.Minute,

View File

@ -37,8 +37,8 @@ func (rcc *configResourceCountClass) GetUniqueNames() int {
return rcc.cfg.UniqueNames return rcc.cfg.UniqueNames
} }
func (rcc *configResourceCountClass) GetFrontends() int { func (rcc *configResourceCountClass) GetShareFrontends() int {
return rcc.cfg.Frontends return rcc.cfg.ShareFrontends
} }
func (rcc *configResourceCountClass) String() string { func (rcc *configResourceCountClass) String() string {

View File

@ -22,7 +22,7 @@ type ResourceCountClass interface {
GetShares() int GetShares() int
GetReservedShares() int GetReservedShares() int
GetUniqueNames() int GetUniqueNames() int
GetFrontends() int GetShareFrontends() int
} }
type BandwidthClass interface { type BandwidthClass interface {
@ -43,7 +43,7 @@ type LimitClass struct {
Shares int Shares int
ReservedShares int ReservedShares int
UniqueNames int UniqueNames int
Frontends int ShareFrontends int
PeriodMinutes int PeriodMinutes int
RxBytes int64 RxBytes int64
TxBytes int64 TxBytes int64
@ -79,8 +79,8 @@ func (lc LimitClass) GetUniqueNames() int {
return lc.UniqueNames return lc.UniqueNames
} }
func (lc LimitClass) GetFrontends() int { func (lc LimitClass) GetShareFrontends() int {
return lc.Frontends return lc.ShareFrontends
} }
func (lc LimitClass) GetBackendMode() sdk.BackendMode { func (lc LimitClass) GetBackendMode() sdk.BackendMode {
@ -127,8 +127,8 @@ func (lc LimitClass) String() string {
if lc.UniqueNames > Unlimited { if lc.UniqueNames > Unlimited {
out += fmt.Sprintf(", uniqueNames: %d", lc.UniqueNames) out += fmt.Sprintf(", uniqueNames: %d", lc.UniqueNames)
} }
if lc.Frontends > Unlimited { if lc.ShareFrontends > Unlimited {
out += fmt.Sprintf(", frontends: %d", lc.Frontends) out += fmt.Sprintf(", frontends: %d", lc.ShareFrontends)
} }
if lc.RxBytes > Unlimited || lc.TxBytes > Unlimited || lc.TotalBytes > Unlimited { if lc.RxBytes > Unlimited || lc.TxBytes > Unlimited || lc.TotalBytes > Unlimited {
out += fmt.Sprintf(", periodMinutes: %d", lc.PeriodMinutes) out += fmt.Sprintf(", periodMinutes: %d", lc.PeriodMinutes)
@ -149,12 +149,12 @@ func (lc LimitClass) String() string {
var _ BandwidthClass = (*LimitClass)(nil) var _ BandwidthClass = (*LimitClass)(nil)
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) { func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
stmt, err := trx.Prepare("insert into limit_classes (backend_mode, environments, shares, reserved_shares, unique_names, frontends, period_minutes, rx_bytes, tx_bytes, total_bytes, limit_action) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning id") stmt, err := trx.Prepare("insert into limit_classes (backend_mode, environments, shares, reserved_shares, unique_names, share_frontends, period_minutes, rx_bytes, tx_bytes, total_bytes, limit_action) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning id")
if err != nil { if err != nil {
return 0, errors.Wrap(err, "error preparing limit_classes insert statement") return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
} }
var id int var id int
if err := stmt.QueryRow(lc.BackendMode, lc.Environments, lc.Shares, lc.ReservedShares, lc.UniqueNames, lc.Frontends, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes, lc.LimitAction).Scan(&id); err != nil { if err := stmt.QueryRow(lc.BackendMode, lc.Environments, lc.Shares, lc.ReservedShares, lc.UniqueNames, lc.ShareFrontends, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes, lc.LimitAction).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing limit_classes insert statement") return 0, errors.Wrap(err, "error executing limit_classes insert statement")
} }
return id, nil return id, nil

View File

@ -1,3 +1,3 @@
-- +migrate Up -- +migrate Up
alter table limit_classes add column frontends int not null default (-1); alter table limit_classes add column share_frontends int not null default (-1);

View File

@ -1,3 +1,3 @@
-- +migrate Up -- +migrate Up
alter table limit_classes add column frontends int not null default (-1); alter table limit_classes add column share_frontends int not null default (-1);