From 25bd423622775d704deccadcc73b2af957f753b3 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 12 Jun 2024 14:16:25 -0400 Subject: [PATCH] fix for relax transient cache with multiple accounts (#606) --- controller/limits/agent.go | 49 ++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/controller/limits/agent.go b/controller/limits/agent.go index 7bb8023f..1d7745cc 100644 --- a/controller/limits/agent.go +++ b/controller/limits/agent.go @@ -330,13 +330,9 @@ func (a *Agent) relax() error { commit := false if bwjes, err := a.str.FindAllBandwidthLimitJournal(trx); err == nil { - periodBw := make(map[int]struct { - rx int64 - tx int64 - }) - accounts := make(map[int]*store.Account) uls := make(map[int]*userLimits) + accountPeriods := make(map[int]map[int]*periodBwValues) for _, bwje := range bwjes { if _, found := accounts[bwje.AccountId]; !found { @@ -347,7 +343,7 @@ func (a *Agent) relax() error { return errors.Wrapf(err, "error getting user limits for '%v'", acct.Email) } uls[bwje.AccountId] = ul - + accountPeriods[bwje.AccountId] = make(map[int]*periodBwValues) } else { return err } @@ -369,21 +365,20 @@ func (a *Agent) relax() error { bwc = lc } - if _, found := periodBw[bwc.GetPeriodMinutes()]; !found { - rx, tx, err := a.ifx.totalRxTxForAccount(int64(bwje.AccountId), time.Duration(bwc.GetPeriodMinutes())*time.Minute) - if err != nil { - return err - } - periodBw[bwc.GetPeriodMinutes()] = struct { - rx int64 - tx int64 - }{ - rx: rx, - tx: tx, + if periods, accountFound := accountPeriods[bwje.AccountId]; accountFound { + if _, periodFound := periods[bwc.GetPeriodMinutes()]; !periodFound { + rx, tx, err := a.ifx.totalRxTxForAccount(int64(bwje.AccountId), time.Duration(bwc.GetPeriodMinutes())*time.Minute) + if err != nil { + return err + } + periods[bwc.GetPeriodMinutes()] = &periodBwValues{rx: rx, tx: tx} + accountPeriods[bwje.AccountId] = periods } + } else { + return errors.New("accountPeriods corrupted") } - used := periodBw[bwc.GetPeriodMinutes()] + used := accountPeriods[bwje.AccountId][bwc.GetPeriodMinutes()] if !a.transferBytesExceeded(used.rx, used.tx, bwc) { if bwc.GetLimitAction() == store.LimitLimitAction { logrus.Infof("relaxing limit '%v' for '%v'", bwc.String(), accounts[bwje.AccountId].Email) @@ -457,10 +452,7 @@ func (a *Agent) isBandwidthClassLimitedForAccount(acctId int, bwc store.Bandwidt } func (a *Agent) anyBandwidthLimitExceeded(acct *store.Account, u *metrics.Usage, bwcs []store.BandwidthClass) (store.BandwidthClass, int64, int64, error) { - periodBw := make(map[int]struct { - rx int64 - tx int64 - }) + periodBw := make(map[int]periodBwValues) var selectedLc store.BandwidthClass var rxBytes int64 @@ -472,13 +464,7 @@ func (a *Agent) anyBandwidthLimitExceeded(acct *store.Account, u *metrics.Usage, if err != nil { return nil, 0, 0, errors.Wrapf(err, "error getting rx/tx for account '%v'", acct.Email) } - periodBw[bwc.GetPeriodMinutes()] = struct { - rx int64 - tx int64 - }{ - rx: rx, - tx: tx, - } + periodBw[bwc.GetPeriodMinutes()] = periodBwValues{rx: rx, tx: tx} } period := periodBw[bwc.GetPeriodMinutes()] @@ -510,3 +496,8 @@ func (a *Agent) transferBytesExceeded(rx, tx int64, bwc store.BandwidthClass) bo } return false } + +type periodBwValues struct { + rx int64 + tx int64 +}