fix for relax transient cache with multiple accounts (#606)

This commit is contained in:
Michael Quigley 2024-06-12 14:16:25 -04:00
parent 59ddb4d199
commit 25bd423622
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -330,13 +330,9 @@ func (a *Agent) relax() error {
commit := false commit := false
if bwjes, err := a.str.FindAllBandwidthLimitJournal(trx); err == nil { if bwjes, err := a.str.FindAllBandwidthLimitJournal(trx); err == nil {
periodBw := make(map[int]struct {
rx int64
tx int64
})
accounts := make(map[int]*store.Account) accounts := make(map[int]*store.Account)
uls := make(map[int]*userLimits) uls := make(map[int]*userLimits)
accountPeriods := make(map[int]map[int]*periodBwValues)
for _, bwje := range bwjes { for _, bwje := range bwjes {
if _, found := accounts[bwje.AccountId]; !found { 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) return errors.Wrapf(err, "error getting user limits for '%v'", acct.Email)
} }
uls[bwje.AccountId] = ul uls[bwje.AccountId] = ul
accountPeriods[bwje.AccountId] = make(map[int]*periodBwValues)
} else { } else {
return err return err
} }
@ -369,21 +365,20 @@ func (a *Agent) relax() error {
bwc = lc bwc = lc
} }
if _, found := periodBw[bwc.GetPeriodMinutes()]; !found { if periods, accountFound := accountPeriods[bwje.AccountId]; accountFound {
rx, tx, err := a.ifx.totalRxTxForAccount(int64(bwje.AccountId), time.Duration(bwc.GetPeriodMinutes())*time.Minute) if _, periodFound := periods[bwc.GetPeriodMinutes()]; !periodFound {
if err != nil { rx, tx, err := a.ifx.totalRxTxForAccount(int64(bwje.AccountId), time.Duration(bwc.GetPeriodMinutes())*time.Minute)
return err if err != nil {
} return err
periodBw[bwc.GetPeriodMinutes()] = struct { }
rx int64 periods[bwc.GetPeriodMinutes()] = &periodBwValues{rx: rx, tx: tx}
tx int64 accountPeriods[bwje.AccountId] = periods
}{
rx: rx,
tx: tx,
} }
} 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 !a.transferBytesExceeded(used.rx, used.tx, bwc) {
if bwc.GetLimitAction() == store.LimitLimitAction { if bwc.GetLimitAction() == store.LimitLimitAction {
logrus.Infof("relaxing limit '%v' for '%v'", bwc.String(), accounts[bwje.AccountId].Email) 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) { func (a *Agent) anyBandwidthLimitExceeded(acct *store.Account, u *metrics.Usage, bwcs []store.BandwidthClass) (store.BandwidthClass, int64, int64, error) {
periodBw := make(map[int]struct { periodBw := make(map[int]periodBwValues)
rx int64
tx int64
})
var selectedLc store.BandwidthClass var selectedLc store.BandwidthClass
var rxBytes int64 var rxBytes int64
@ -472,13 +464,7 @@ func (a *Agent) anyBandwidthLimitExceeded(acct *store.Account, u *metrics.Usage,
if err != nil { if err != nil {
return nil, 0, 0, errors.Wrapf(err, "error getting rx/tx for account '%v'", acct.Email) return nil, 0, 0, errors.Wrapf(err, "error getting rx/tx for account '%v'", acct.Email)
} }
periodBw[bwc.GetPeriodMinutes()] = struct { periodBw[bwc.GetPeriodMinutes()] = periodBwValues{rx: rx, tx: tx}
rx int64
tx int64
}{
rx: rx,
tx: tx,
}
} }
period := periodBw[bwc.GetPeriodMinutes()] period := periodBw[bwc.GetPeriodMinutes()]
@ -510,3 +496,8 @@ func (a *Agent) transferBytesExceeded(rx, tx int64, bwc store.BandwidthClass) bo
} }
return false return false
} }
type periodBwValues struct {
rx int64
tx int64
}