mirror of
https://github.com/openziti/zrok.git
synced 2025-03-14 23:48:22 +01:00
refactoring limits selection into a new userLimits structure (#606)
This commit is contained in:
parent
7b8c9483e7
commit
a9dff531fc
@ -92,7 +92,7 @@ func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if maxEnvironments > Unlimited {
|
||||
if maxEnvironments > store.Unlimited {
|
||||
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@ -155,7 +155,7 @@ func (a *Agent) CanCreateShare(acctId, envId int, reserved, uniqueName bool, sha
|
||||
}
|
||||
}
|
||||
|
||||
if maxShares > Unlimited || (reserved && maxReservedShares > Unlimited) || (reserved && uniqueName && maxUniqueNames > Unlimited) {
|
||||
if maxShares > store.Unlimited || (reserved && maxReservedShares > store.Unlimited) || (reserved && uniqueName && maxUniqueNames > store.Unlimited) {
|
||||
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@ -577,13 +577,13 @@ func (a *Agent) bandwidthClassPoints(bwc store.BandwidthClass) int {
|
||||
}
|
||||
|
||||
func (a *Agent) limitExceeded(rx, tx int64, bwc store.BandwidthClass) bool {
|
||||
if bwc.GetTxBytes() != Unlimited && tx >= bwc.GetTxBytes() {
|
||||
if bwc.GetTxBytes() != store.Unlimited && tx >= bwc.GetTxBytes() {
|
||||
return true
|
||||
}
|
||||
if bwc.GetRxBytes() != Unlimited && rx >= bwc.GetRxBytes() {
|
||||
if bwc.GetRxBytes() != store.Unlimited && rx >= bwc.GetRxBytes() {
|
||||
return true
|
||||
}
|
||||
if bwc.GetTotalBytes() != Unlimited && tx+rx >= bwc.GetTotalBytes() {
|
||||
if bwc.GetTotalBytes() != store.Unlimited && tx+rx >= bwc.GetTotalBytes() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
@ -1,8 +1,9 @@
|
||||
package limits
|
||||
|
||||
import "time"
|
||||
|
||||
const Unlimited = -1
|
||||
import (
|
||||
"github.com/openziti/zrok/controller/store"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Environments int
|
||||
@ -30,24 +31,24 @@ func DefaultBandwidthPerPeriod() *BandwidthPerPeriod {
|
||||
return &BandwidthPerPeriod{
|
||||
Period: 24 * time.Hour,
|
||||
Warning: &Bandwidth{
|
||||
Rx: Unlimited,
|
||||
Tx: Unlimited,
|
||||
Total: Unlimited,
|
||||
Rx: store.Unlimited,
|
||||
Tx: store.Unlimited,
|
||||
Total: store.Unlimited,
|
||||
},
|
||||
Limit: &Bandwidth{
|
||||
Rx: Unlimited,
|
||||
Tx: Unlimited,
|
||||
Total: Unlimited,
|
||||
Rx: store.Unlimited,
|
||||
Tx: store.Unlimited,
|
||||
Total: store.Unlimited,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Environments: Unlimited,
|
||||
Shares: Unlimited,
|
||||
ReservedShares: Unlimited,
|
||||
UniqueNames: Unlimited,
|
||||
Environments: store.Unlimited,
|
||||
Shares: store.Unlimited,
|
||||
ReservedShares: store.Unlimited,
|
||||
UniqueNames: store.Unlimited,
|
||||
Bandwidth: DefaultBandwidthPerPeriod(),
|
||||
Enforcing: false,
|
||||
Cycle: 15 * time.Minute,
|
||||
|
42
controller/limits/resourceCountClass.go
Normal file
42
controller/limits/resourceCountClass.go
Normal file
@ -0,0 +1,42 @@
|
||||
package limits
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/openziti/zrok/controller/store"
|
||||
)
|
||||
|
||||
type configResourceCountClass struct {
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
func newConfigResourceCountClass(cfg *Config) store.ResourceCountClass {
|
||||
return &configResourceCountClass{cfg}
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) IsGlobal() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) GetLimitClassId() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) GetEnvironments() int {
|
||||
return rcc.cfg.Environments
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) GetShares() int {
|
||||
return rcc.cfg.Shares
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) GetReservedShares() int {
|
||||
return rcc.cfg.ReservedShares
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) GetUniqueNames() int {
|
||||
return rcc.cfg.UniqueNames
|
||||
}
|
||||
|
||||
func (rcc *configResourceCountClass) String() string {
|
||||
return fmt.Sprintf("Config<environments: %d, shares: %d, reservedShares: %d, uniqueNames: %d>", rcc.cfg.Environments, rcc.cfg.Shares, rcc.cfg.ReservedShares, rcc.cfg.UniqueNames)
|
||||
}
|
@ -13,5 +13,7 @@ type userLimits struct {
|
||||
}
|
||||
|
||||
func (a *Agent) getUserLimits(acctId int, trx *sqlx.Tx) (*userLimits, error) {
|
||||
return nil, nil
|
||||
_ = newConfigBandwidthClasses(a.cfg.Bandwidth)
|
||||
userLimits := &userLimits{}
|
||||
return userLimits, nil
|
||||
}
|
||||
|
@ -23,16 +23,16 @@ func (a *warningAction) HandleAccount(acct *store.Account, rxBytes, txBytes int6
|
||||
logrus.Infof("warning '%v'", acct.Email)
|
||||
|
||||
if a.cfg != nil {
|
||||
rxLimit := "(unlimited bytes)"
|
||||
if limit.GetRxBytes() != Unlimited {
|
||||
rxLimit := "(store.Unlimited bytes)"
|
||||
if limit.GetRxBytes() != store.Unlimited {
|
||||
rxLimit = util.BytesToSize(limit.GetRxBytes())
|
||||
}
|
||||
txLimit := "(unlimited bytes)"
|
||||
if limit.GetTxBytes() != Unlimited {
|
||||
txLimit := "(store.Unlimited bytes)"
|
||||
if limit.GetTxBytes() != store.Unlimited {
|
||||
txLimit = util.BytesToSize(limit.GetTxBytes())
|
||||
}
|
||||
totalLimit := "(unlimited bytes)"
|
||||
if limit.GetTotalBytes() != Unlimited {
|
||||
totalLimit := "(store.Unlimited bytes)"
|
||||
if limit.GetTotalBytes() != store.Unlimited {
|
||||
totalLimit = util.BytesToSize(limit.GetTotalBytes())
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/openziti/zrok/sdk/golang/sdk"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const Unlimited = -1
|
||||
|
||||
type ResourceCountClass interface {
|
||||
IsGlobal() bool
|
||||
GetLimitClassId() int
|
||||
@ -14,7 +16,7 @@ type ResourceCountClass interface {
|
||||
GetShares() int
|
||||
GetReservedShares() int
|
||||
GetUniqueNames() int
|
||||
String()
|
||||
String() string
|
||||
}
|
||||
|
||||
type BandwidthClass interface {
|
||||
@ -52,6 +54,22 @@ func (lc LimitClass) GetLimitClassId() int {
|
||||
return lc.Id
|
||||
}
|
||||
|
||||
func (lc LimitClass) GetEnvironments() int {
|
||||
return lc.Environments
|
||||
}
|
||||
|
||||
func (lc LimitClass) GetShares() int {
|
||||
return lc.Shares
|
||||
}
|
||||
|
||||
func (lc LimitClass) GetReservedShares() int {
|
||||
return lc.ReservedShares
|
||||
}
|
||||
|
||||
func (lc LimitClass) GetUniqueNames() int {
|
||||
return lc.UniqueNames
|
||||
}
|
||||
|
||||
func (lc LimitClass) GetShareMode() sdk.ShareMode {
|
||||
if lc.ShareMode == nil {
|
||||
return ""
|
||||
@ -87,10 +105,39 @@ func (lc LimitClass) GetLimitAction() LimitAction {
|
||||
}
|
||||
|
||||
func (lc LimitClass) String() string {
|
||||
if out, err := json.Marshal(&lc); err == nil {
|
||||
return "LimitClass<" + string(out) + ">"
|
||||
out := fmt.Sprintf("LimitClass<%d", lc.Id)
|
||||
if lc.ShareMode != nil {
|
||||
out += fmt.Sprintf(", shareMode: '%s'", *lc.ShareMode)
|
||||
}
|
||||
return "<<ERROR>>"
|
||||
if lc.BackendMode != nil {
|
||||
out += fmt.Sprintf(", backendMode: '%s'", *lc.BackendMode)
|
||||
}
|
||||
if lc.Environments > Unlimited {
|
||||
out += fmt.Sprintf(", environments: %d", lc.Environments)
|
||||
}
|
||||
if lc.Shares > Unlimited {
|
||||
out += fmt.Sprintf(", shares: %d", lc.Shares)
|
||||
}
|
||||
if lc.ReservedShares > Unlimited {
|
||||
out += fmt.Sprintf(", reservedShares: %d", lc.ReservedShares)
|
||||
}
|
||||
if lc.UniqueNames > Unlimited {
|
||||
out += fmt.Sprintf(", uniqueNames: %d", lc.UniqueNames)
|
||||
}
|
||||
if lc.RxBytes > Unlimited || lc.TxBytes > Unlimited || lc.TotalBytes > Unlimited {
|
||||
out += fmt.Sprintf(", periodMinutes: %d", lc.PeriodMinutes)
|
||||
}
|
||||
if lc.RxBytes > Unlimited {
|
||||
out += fmt.Sprintf(", rxBytes: %d", lc.RxBytes)
|
||||
}
|
||||
if lc.TxBytes > Unlimited {
|
||||
out += fmt.Sprintf(", txBytes: %d", lc.TxBytes)
|
||||
}
|
||||
if lc.TotalBytes > Unlimited {
|
||||
out += fmt.Sprintf(", totalBytes: %d", lc.TotalBytes)
|
||||
}
|
||||
out += fmt.Sprintf(", limitAction: '%v'>", lc.LimitAction)
|
||||
return out
|
||||
}
|
||||
|
||||
var _ BandwidthClass = (*LimitClass)(nil)
|
||||
|
Loading…
Reference in New Issue
Block a user