mirror of
https://github.com/openziti/zrok.git
synced 2024-12-22 23:02:52 +01:00
new bandwidth class interface to facilitate operating on global and class-based bandwidth limits in the same code, easily (#606)
This commit is contained in:
parent
cea7ff6474
commit
8a255a0ee8
@ -98,16 +98,6 @@ func (a *Agent) CanCreateShare(acctId, envId int, reserved, uniqueName bool, _ s
|
||||
return false, err
|
||||
}
|
||||
|
||||
alc, err := a.str.FindLimitClassesForAccount(acctId, trx)
|
||||
if err != nil {
|
||||
logrus.Errorf("error finding limit classes for account with id '%d': %v", acctId, err)
|
||||
return false, err
|
||||
}
|
||||
sortLimitClasses(alc)
|
||||
if len(alc) > 0 {
|
||||
logrus.Infof("selected limit class: %v", alc[0])
|
||||
}
|
||||
|
||||
if a.cfg.Shares > Unlimited || (reserved && a.cfg.ReservedShares > Unlimited) || (reserved && uniqueName && a.cfg.UniqueNames > Unlimited) {
|
||||
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
|
||||
if err != nil {
|
||||
|
@ -2,22 +2,53 @@ package limits
|
||||
|
||||
import (
|
||||
"github.com/openziti/zrok/controller/store"
|
||||
"sort"
|
||||
"github.com/openziti/zrok/sdk/golang/sdk"
|
||||
)
|
||||
|
||||
func sortLimitClasses(lcs []*store.LimitClass) {
|
||||
sort.Slice(lcs, func(i, j int) bool {
|
||||
return modePoints(lcs[i]) > modePoints(lcs[j])
|
||||
})
|
||||
type configBandwidthClass struct {
|
||||
periodInMinutes int
|
||||
bw *Bandwidth
|
||||
limitAction store.LimitAction
|
||||
}
|
||||
|
||||
func modePoints(lc *store.LimitClass) int {
|
||||
points := 0
|
||||
if lc.BackendMode != "" {
|
||||
points += 1
|
||||
func newConfigBandwidthClasses(cfgClass *BandwidthPerPeriod) []store.BandwidthClass {
|
||||
return []store.BandwidthClass{
|
||||
&configBandwidthClass{
|
||||
periodInMinutes: int(cfgClass.Period.Minutes()),
|
||||
bw: cfgClass.Warning,
|
||||
limitAction: store.WarningLimitAction,
|
||||
},
|
||||
}
|
||||
if lc.ShareMode != "" {
|
||||
points += 1
|
||||
}
|
||||
return points
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) IsGlobal() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetShareMode() sdk.ShareMode {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetBackendMode() sdk.BackendMode {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetPeriodMinutes() int {
|
||||
return bc.periodInMinutes
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetRxBytes() int64 {
|
||||
return bc.bw.Rx
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetTxBytes() int64 {
|
||||
return bc.bw.Tx
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetTotalBytes() int64 {
|
||||
return bc.bw.Total
|
||||
}
|
||||
|
||||
func (bc *configBandwidthClass) GetLimitAction() store.LimitAction {
|
||||
return bc.limitAction
|
||||
}
|
||||
|
@ -7,6 +7,17 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type BandwidthClass interface {
|
||||
IsGlobal() bool
|
||||
GetShareMode() sdk.ShareMode
|
||||
GetBackendMode() sdk.BackendMode
|
||||
GetPeriodMinutes() int
|
||||
GetRxBytes() int64
|
||||
GetTxBytes() int64
|
||||
GetTotalBytes() int64
|
||||
GetLimitAction() LimitAction
|
||||
}
|
||||
|
||||
type LimitClass struct {
|
||||
Model
|
||||
ShareMode sdk.ShareMode
|
||||
@ -22,6 +33,38 @@ type LimitClass struct {
|
||||
LimitAction LimitAction
|
||||
}
|
||||
|
||||
func (lc *LimitClass) IsGlobal() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetShareMode() sdk.ShareMode {
|
||||
return lc.ShareMode
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetBackendMode() sdk.BackendMode {
|
||||
return lc.BackendMode
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetPeriodMinutes() int {
|
||||
return lc.PeriodMinutes
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetRxBytes() int64 {
|
||||
return lc.RxBytes
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetTxBytes() int64 {
|
||||
return lc.TxBytes
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetTotalBytes() int64 {
|
||||
return lc.TotalBytes
|
||||
}
|
||||
|
||||
func (lc *LimitClass) GetLimitAction() LimitAction {
|
||||
return lc.LimitAction
|
||||
}
|
||||
|
||||
func (lc LimitClass) String() string {
|
||||
out, err := json.MarshalIndent(&lc, "", " ")
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user