-
Notifications
You must be signed in to change notification settings - Fork 89
/
limitClass.go
160 lines (137 loc) · 3.91 KB
/
limitClass.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package store
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/util"
"github.com/pkg/errors"
)
const Unlimited = -1
type BaseLimitClass interface {
IsGlobal() bool
GetLimitClassId() int
String() string
}
type ResourceCountClass interface {
BaseLimitClass
GetEnvironments() int
GetShares() int
GetReservedShares() int
GetUniqueNames() int
}
type BandwidthClass interface {
BaseLimitClass
IsScoped() bool
GetBackendMode() sdk.BackendMode
GetPeriodMinutes() int
GetRxBytes() int64
GetTxBytes() int64
GetTotalBytes() int64
GetLimitAction() LimitAction
}
type LimitClass struct {
Model
BackendMode *sdk.BackendMode
Environments int
Shares int
ReservedShares int
UniqueNames int
PeriodMinutes int
RxBytes int64
TxBytes int64
TotalBytes int64
LimitAction LimitAction
}
func (lc LimitClass) IsGlobal() bool {
return false
}
func (lc LimitClass) IsScoped() bool {
return lc.BackendMode != nil
}
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) GetBackendMode() sdk.BackendMode {
if lc.BackendMode == nil {
return ""
}
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 := fmt.Sprintf("LimitClass<#%d", lc.Id)
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: %v", util.BytesToSize(lc.RxBytes))
}
if lc.TxBytes > Unlimited {
out += fmt.Sprintf(", txBytes: %v", util.BytesToSize(lc.TxBytes))
}
if lc.TotalBytes > Unlimited {
out += fmt.Sprintf(", totalBytes: %v", util.BytesToSize(lc.TotalBytes))
}
out += fmt.Sprintf(", limitAction: '%v'>", lc.LimitAction)
return out
}
var _ BandwidthClass = (*LimitClass)(nil)
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
stmt, err := trx.Prepare("insert into limit_classes (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) returning id")
if err != nil {
return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
}
var id int
if err := stmt.QueryRow(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
}
func (str *Store) GetLimitClass(lcId int, trx *sqlx.Tx) (*LimitClass, error) {
lc := &LimitClass{}
if err := trx.QueryRowx("select * from limit_classes where id = $1", lcId).StructScan(lc); err != nil {
return nil, errors.Wrap(err, "error selecting limit_class by id")
}
return lc, nil
}