refinements and refactoring of limit classes to re-align with updated bandwidth journal, etc. (#606)

This commit is contained in:
Michael Quigley 2024-06-03 13:37:32 -04:00
parent 6256055a99
commit cea7ff6474
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
6 changed files with 34 additions and 53 deletions

View File

@ -19,9 +19,9 @@ type Agent struct {
zCfg *zrokEdgeSdk.Config
str *store.Store
queue chan *metrics.Usage
acctWarningActions []AccountAction
acctLimitActions []AccountAction
acctRelaxActions []AccountAction
warningActions []AccountAction
limitActions []AccountAction
relaxActions []AccountAction
close chan struct{}
join chan struct{}
}
@ -33,9 +33,9 @@ func NewAgent(cfg *Config, ifxCfg *metrics.InfluxConfig, zCfg *zrokEdgeSdk.Confi
zCfg: zCfg,
str: str,
queue: make(chan *metrics.Usage, 1024),
acctWarningActions: []AccountAction{newAccountWarningAction(emailCfg, str)},
acctLimitActions: []AccountAction{newAccountLimitAction(str, zCfg)},
acctRelaxActions: []AccountAction{newAccountRelaxAction(str, zCfg)},
warningActions: []AccountAction{newAccountWarningAction(emailCfg, str)},
limitActions: []AccountAction{newAccountLimitAction(str, zCfg)},
relaxActions: []AccountAction{newAccountRelaxAction(str, zCfg)},
close: make(chan struct{}),
join: make(chan struct{}),
}
@ -258,8 +258,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
if err != nil {
return err
}
// run account limit actions
for _, action := range a.acctLimitActions {
for _, action := range a.limitActions {
if err := action.HandleAccount(acct, rxBytes, txBytes, a.cfg.Bandwidth, trx); err != nil {
return errors.Wrapf(err, "%v", reflect.TypeOf(action).String())
}
@ -295,8 +294,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
if err != nil {
return err
}
// run account warning actions
for _, action := range a.acctWarningActions {
for _, action := range a.warningActions {
if err := action.HandleAccount(acct, rxBytes, txBytes, a.cfg.Bandwidth, trx); err != nil {
return errors.Wrapf(err, "%v", reflect.TypeOf(action).String())
}
@ -334,7 +332,7 @@ func (a *Agent) relax() error {
if !enforce && !warning {
if alj.Action == store.LimitLimitAction {
// run relax actions for account
for _, action := range a.acctRelaxActions {
for _, action := range a.relaxActions {
if err := action.HandleAccount(acct, rxBytes, txBytes, a.cfg.Bandwidth, trx); err != nil {
return errors.Wrapf(err, "%v", reflect.TypeOf(action).String())
}

View File

@ -7,25 +7,10 @@ import (
func sortLimitClasses(lcs []*store.LimitClass) {
sort.Slice(lcs, func(i, j int) bool {
ipoints := limitScopePoints(lcs[i]) + modePoints(lcs[i])
jpoints := limitScopePoints(lcs[j]) + modePoints(lcs[j])
return ipoints > jpoints
return modePoints(lcs[i]) > modePoints(lcs[j])
})
}
func limitScopePoints(lc *store.LimitClass) int {
points := 0
switch lc.LimitScope {
case store.AccountLimitScope:
points += 1000
case store.EnvironmentLimitScope:
points += 100
case store.ShareLimitScope:
points += 10
}
return points
}
func modePoints(lc *store.LimitClass) int {
points := 0
if lc.BackendMode != "" {

View File

@ -38,14 +38,13 @@ func TestBandwidthLimitJournal(t *testing.T) {
assert.Equal(t, int64(2048), latestJe.TxBytes)
lcId, err := str.CreateLimitClass(&LimitClass{
LimitScope: AccountLimitScope,
LimitAction: LimitLimitAction,
ShareMode: sdk.PrivateShareMode,
BackendMode: sdk.VpnBackendMode,
PeriodMinutes: 60,
RxBytes: 4096,
TxBytes: 8192,
TotalBytes: 10240,
LimitAction: LimitLimitAction,
}, trx)
assert.NoError(t, err)

View File

@ -9,10 +9,9 @@ import (
type LimitClass struct {
Model
LimitScope LimitScope
LimitAction LimitAction
ShareMode sdk.ShareMode
BackendMode sdk.BackendMode
Environments int
Shares int
ReservedShares int
UniqueNames int
@ -20,6 +19,7 @@ type LimitClass struct {
RxBytes int64
TxBytes int64
TotalBytes int64
LimitAction LimitAction
}
func (lc LimitClass) String() string {
@ -32,12 +32,12 @@ func (lc LimitClass) String() string {
}
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
stmt, err := trx.Prepare("insert into limit_classes (limit_scope, limit_action, share_mode, backend_mode, period_minutes, rx_bytes, tx_bytes, total_bytes) values ($1, $2, $3, $4, $5, $6, $7, $8) returning id")
stmt, err := trx.Prepare("insert into limit_classes (share_mode, backend_mode, environments, shares, reserved_shares, unique_names, period_minutes, rx_bytes, tx_bytes, total_bytes, limit_action) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning id")
if err != nil {
return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
}
var id int
if err := stmt.QueryRow(lc.LimitScope, lc.LimitAction, lc.ShareMode, lc.BackendMode, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes).Scan(&id); err != nil {
if err := stmt.QueryRow(lc.ShareMode, lc.BackendMode, lc.Environments, lc.Shares, lc.ReservedShares, lc.UniqueNames, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes, lc.LimitAction).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing limit_classes insert statement")
}
return id, nil

View File

@ -1,13 +1,10 @@
-- +migrate Up
create type limit_scope as enum ('account', 'environment', 'share');
create type limit_action as enum ('warning', 'limit');
create table limit_classes (
id serial primary key,
limit_scope limit_scope not null default ('account'),
limit_action limit_action not null default ('limit'),
share_mode share_mode,
backend_mode backend_mode,
@ -20,6 +17,8 @@ create table limit_classes (
tx_bytes bigint not null default (-1),
total_bytes bigint not null default (-1),
limit_action limit_action not null default ('limit'),
created_at timestamptz not null default(current_timestamp),
updated_at timestamptz not null default(current_timestamp),
deleted boolean not null default(false)

View File

@ -3,8 +3,6 @@
create table limit_classes (
id integer primary key,
limit_scope string not null default ('account'),
limit_action string not null default ('limit'),
share_mode string,
backend_mode string,
@ -17,6 +15,8 @@ create table limit_classes (
tx_bytes bigint not null default (-1),
total_bytes bigint not null default (-1),
limit_action string not null default ('limit'),
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
deleted boolean not null default(false)