-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathenforcer.go
164 lines (141 loc) · 4.39 KB
/
enforcer.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
161
162
163
164
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cost
import (
"fmt"
"github.com/uber-go/tally"
)
const (
defaultCostExceededErrorFmt = "%s exceeds limit of %s"
customCostExceededErrorFmt = "%s exceeds limit of %s: %s"
)
var (
noopManager = NewStaticLimitManager(
NewLimitManagerOptions().
SetDefaultLimit(Limit{
Threshold: maxCost,
Enabled: false,
},
),
)
noopEnforcer = NewEnforcer(noopManager, NewNoopTracker(), nil)
)
// Report is a report on the cost limits of an enforcer.
type Report struct {
Cost
Error error
}
// enforcer enforces cost limits for operations.
type enforcer struct {
LimitManager
tracker Tracker
costMsg string
metrics enforcerMetrics
}
// NewEnforcer returns a new enforcer for cost limits.
func NewEnforcer(m LimitManager, t Tracker, opts EnforcerOptions) Enforcer {
if opts == nil {
opts = NewEnforcerOptions()
}
return &enforcer{
LimitManager: m,
tracker: t,
costMsg: opts.CostExceededMessage(),
metrics: newEnforcerMetrics(opts.InstrumentOptions().MetricsScope(), opts.ValueBuckets()),
}
}
// Add adds the cost of an operation to the enforcer's current total. If the operation exceeds
// the enforcer's limit the enforcer will return a CostLimit error in addition to the new total.
func (e *enforcer) Add(cost Cost) Report {
current := e.tracker.Add(cost)
limit := e.Limit()
overLimit := e.checkLimit(current, limit)
if overLimit != nil {
// Emit metrics on number of operations that are over the limit even when not enabled.
e.metrics.overLimit.Inc(1)
if limit.Enabled {
e.metrics.overLimitAndEnabled.Inc(1)
}
}
return Report{
Cost: current,
Error: overLimit,
}
}
// State returns the current state of the enforcer.
func (e *enforcer) State() (Report, Limit) {
cost := e.tracker.Current()
l := e.Limit()
err := e.checkLimit(cost, l)
r := Report{
Cost: cost,
Error: err,
}
return r, l
}
// Clone clones the current enforcer. The new enforcer uses the same Estimator and LimitManager
// as e buts its Tracker is independent.
func (e *enforcer) Clone() Enforcer {
return &enforcer{
LimitManager: e.LimitManager,
tracker: NewTracker(),
costMsg: e.costMsg,
metrics: e.metrics,
}
}
func (e *enforcer) checkLimit(cost Cost, limit Limit) error {
if cost < limit.Threshold || !limit.Enabled {
return nil
}
if e.costMsg == "" {
return defaultCostExceededError(cost, limit)
}
return costExceededError(e.costMsg, cost, limit)
}
func defaultCostExceededError(cost Cost, limit Limit) error {
return fmt.Errorf(
defaultCostExceededErrorFmt,
fmt.Sprintf("%v", float64(cost)),
fmt.Sprintf("%v", float64(limit.Threshold)),
)
}
func costExceededError(customMessage string, cost Cost, limit Limit) error {
return fmt.Errorf(
customCostExceededErrorFmt,
fmt.Sprintf("%v", float64(cost)),
fmt.Sprintf("%v", float64(limit.Threshold)),
customMessage,
)
}
// NoopEnforcer returns a new enforcer that always returns a current cost of 0 and
// is always disabled.
func NoopEnforcer() Enforcer {
return noopEnforcer
}
type enforcerMetrics struct {
overLimit tally.Counter
overLimitAndEnabled tally.Counter
}
func newEnforcerMetrics(s tally.Scope, b tally.ValueBuckets) enforcerMetrics {
return enforcerMetrics{
overLimit: s.Counter("over-limit"),
overLimitAndEnabled: s.Counter("over-limit-and-enabled"),
}
}