use the new userLimits structure to find the right bandwidth class (#606)

This commit is contained in:
Michael Quigley 2024-06-05 16:54:05 -04:00
parent 56252b6e0a
commit b511eeee17
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -313,17 +313,17 @@ func (a *Agent) enforce(u *metrics.Usage) error {
return nil return nil
} }
shr, err := a.str.FindShareWithTokenEvenIfDeleted(u.ShareToken, trx) //shr, err := a.str.FindShareWithTokenEvenIfDeleted(u.ShareToken, trx)
if err != nil { //if err != nil {
return err // return err
} //}
logrus.Debugf("share: '%v', shareMode: '%v', backendMode: '%v'", shr.Token, shr.ShareMode, shr.BackendMode)
alcs, err := a.str.FindAppliedLimitClassesForAccount(int(u.AccountId), trx) ul, err := a.getUserLimits(int(u.AccountId), trx)
if err != nil { if err != nil {
return err return err
} }
exceededLc, rxBytes, txBytes, err := a.isOverLimitClass(u, alcs)
exceededLc, rxBytes, txBytes, err := a.isOverLimitClass(u, ul.bandwidth)
if err != nil { if err != nil {
return errors.Wrap(err, "error checking limit classes") return errors.Wrap(err, "error checking limit classes")
} }
@ -504,22 +504,17 @@ func (a *Agent) relax() error {
return nil return nil
} }
func (a *Agent) isOverLimitClass(u *metrics.Usage, alcs []*store.LimitClass) (store.BandwidthClass, int64, int64, error) { func (a *Agent) isOverLimitClass(u *metrics.Usage, bwcs []store.BandwidthClass) (store.BandwidthClass, int64, int64, error) {
periodBw := make(map[int]struct { periodBw := make(map[int]struct {
rx int64 rx int64
tx int64 tx int64
}) })
var allBwcs []store.BandwidthClass var selectedLc store.BandwidthClass
for _, alc := range alcs { var rxBytes int64
allBwcs = append(allBwcs, alc) var txBytes int64
}
for _, globBwc := range newConfigBandwidthClasses(a.cfg.Bandwidth) {
allBwcs = append(allBwcs, globBwc)
}
// find period data for each class for _, bwc := range bwcs {
for _, bwc := range allBwcs {
if _, found := periodBw[bwc.GetPeriodMinutes()]; !found { if _, found := periodBw[bwc.GetPeriodMinutes()]; !found {
rx, tx, err := a.ifx.totalRxTxForAccount(u.AccountId, time.Minute*time.Duration(bwc.GetPeriodMinutes())) rx, tx, err := a.ifx.totalRxTxForAccount(u.AccountId, time.Minute*time.Duration(bwc.GetPeriodMinutes()))
if err != nil { if err != nil {
@ -533,27 +528,19 @@ func (a *Agent) isOverLimitClass(u *metrics.Usage, alcs []*store.LimitClass) (st
tx: tx, tx: tx,
} }
} }
period := periodBw[bwc.GetPeriodMinutes()]
if a.limitExceeded(period.rx, period.tx, bwc) {
selectedLc = bwc
rxBytes = period.rx
txBytes = period.tx
} else {
logrus.Debugf("limit ok '%v' with rx: %d, tx: %d, total: %d", bwc, period.rx, period.tx, period.rx+period.tx)
}
} }
// find the highest, most specific limit class that has been exceeded if selectedLc != nil {
var selectedLc store.BandwidthClass logrus.Infof("exceeded limit '%v' with rx: %d, tx: %d, total: %d", selectedLc, rxBytes, txBytes, rxBytes+txBytes)
selectedLcPoints := -1
var rxBytes int64
var txBytes int64
for _, bwc := range allBwcs {
points := a.bandwidthClassPoints(bwc)
if points >= selectedLcPoints {
period := periodBw[bwc.GetPeriodMinutes()]
if a.limitExceeded(period.rx, period.tx, bwc) {
selectedLc = bwc
selectedLcPoints = points
rxBytes = period.rx
txBytes = period.tx
logrus.Debugf("exceeded limit '%v' with rx: %d, tx: %d", bwc.String(), period.rx, period.tx)
} else {
logrus.Debugf("limit '%v' ok with rx: %d, tx: %d", bwc.String(), period.rx, period.tx)
}
}
} }
return selectedLc, rxBytes, txBytes, nil return selectedLc, rxBytes, txBytes, nil