mirror of
https://github.com/openziti/zrok.git
synced 2025-06-21 02:07:44 +02: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)
|
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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)
|
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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 {
|
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
|
return true
|
||||||
}
|
}
|
||||||
if bwc.GetRxBytes() != Unlimited && rx >= bwc.GetRxBytes() {
|
if bwc.GetRxBytes() != store.Unlimited && rx >= bwc.GetRxBytes() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if bwc.GetTotalBytes() != Unlimited && tx+rx >= bwc.GetTotalBytes() {
|
if bwc.GetTotalBytes() != store.Unlimited && tx+rx >= bwc.GetTotalBytes() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package limits
|
package limits
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"github.com/openziti/zrok/controller/store"
|
||||||
const Unlimited = -1
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Environments int
|
Environments int
|
||||||
@ -30,24 +31,24 @@ func DefaultBandwidthPerPeriod() *BandwidthPerPeriod {
|
|||||||
return &BandwidthPerPeriod{
|
return &BandwidthPerPeriod{
|
||||||
Period: 24 * time.Hour,
|
Period: 24 * time.Hour,
|
||||||
Warning: &Bandwidth{
|
Warning: &Bandwidth{
|
||||||
Rx: Unlimited,
|
Rx: store.Unlimited,
|
||||||
Tx: Unlimited,
|
Tx: store.Unlimited,
|
||||||
Total: Unlimited,
|
Total: store.Unlimited,
|
||||||
},
|
},
|
||||||
Limit: &Bandwidth{
|
Limit: &Bandwidth{
|
||||||
Rx: Unlimited,
|
Rx: store.Unlimited,
|
||||||
Tx: Unlimited,
|
Tx: store.Unlimited,
|
||||||
Total: Unlimited,
|
Total: store.Unlimited,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Environments: Unlimited,
|
Environments: store.Unlimited,
|
||||||
Shares: Unlimited,
|
Shares: store.Unlimited,
|
||||||
ReservedShares: Unlimited,
|
ReservedShares: store.Unlimited,
|
||||||
UniqueNames: Unlimited,
|
UniqueNames: store.Unlimited,
|
||||||
Bandwidth: DefaultBandwidthPerPeriod(),
|
Bandwidth: DefaultBandwidthPerPeriod(),
|
||||||
Enforcing: false,
|
Enforcing: false,
|
||||||
Cycle: 15 * time.Minute,
|
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) {
|
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)
|
logrus.Infof("warning '%v'", acct.Email)
|
||||||
|
|
||||||
if a.cfg != nil {
|
if a.cfg != nil {
|
||||||
rxLimit := "(unlimited bytes)"
|
rxLimit := "(store.Unlimited bytes)"
|
||||||
if limit.GetRxBytes() != Unlimited {
|
if limit.GetRxBytes() != store.Unlimited {
|
||||||
rxLimit = util.BytesToSize(limit.GetRxBytes())
|
rxLimit = util.BytesToSize(limit.GetRxBytes())
|
||||||
}
|
}
|
||||||
txLimit := "(unlimited bytes)"
|
txLimit := "(store.Unlimited bytes)"
|
||||||
if limit.GetTxBytes() != Unlimited {
|
if limit.GetTxBytes() != store.Unlimited {
|
||||||
txLimit = util.BytesToSize(limit.GetTxBytes())
|
txLimit = util.BytesToSize(limit.GetTxBytes())
|
||||||
}
|
}
|
||||||
totalLimit := "(unlimited bytes)"
|
totalLimit := "(store.Unlimited bytes)"
|
||||||
if limit.GetTotalBytes() != Unlimited {
|
if limit.GetTotalBytes() != store.Unlimited {
|
||||||
totalLimit = util.BytesToSize(limit.GetTotalBytes())
|
totalLimit = util.BytesToSize(limit.GetTotalBytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/openziti/zrok/sdk/golang/sdk"
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const Unlimited = -1
|
||||||
|
|
||||||
type ResourceCountClass interface {
|
type ResourceCountClass interface {
|
||||||
IsGlobal() bool
|
IsGlobal() bool
|
||||||
GetLimitClassId() int
|
GetLimitClassId() int
|
||||||
@ -14,7 +16,7 @@ type ResourceCountClass interface {
|
|||||||
GetShares() int
|
GetShares() int
|
||||||
GetReservedShares() int
|
GetReservedShares() int
|
||||||
GetUniqueNames() int
|
GetUniqueNames() int
|
||||||
String()
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BandwidthClass interface {
|
type BandwidthClass interface {
|
||||||
@ -52,6 +54,22 @@ func (lc LimitClass) GetLimitClassId() int {
|
|||||||
return lc.Id
|
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 {
|
func (lc LimitClass) GetShareMode() sdk.ShareMode {
|
||||||
if lc.ShareMode == nil {
|
if lc.ShareMode == nil {
|
||||||
return ""
|
return ""
|
||||||
@ -87,10 +105,39 @@ func (lc LimitClass) GetLimitAction() LimitAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc LimitClass) String() string {
|
func (lc LimitClass) String() string {
|
||||||
if out, err := json.Marshal(&lc); err == nil {
|
out := fmt.Sprintf("LimitClass<%d", lc.Id)
|
||||||
return "LimitClass<" + string(out) + ">"
|
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)
|
var _ BandwidthClass = (*LimitClass)(nil)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user