-
Notifications
You must be signed in to change notification settings - Fork 453
/
types.go
85 lines (71 loc) · 2.83 KB
/
types.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
// 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 is a library providing utilities for estimating the cost of operations
// and enforcing limits on them. The primary type of interest is Enforcer, which tracks the cost of operations,
// and errors when that cost goes over a particular limit.
package cost
import (
"math"
)
// Cost represents the cost of an operation.
type Cost float64
const (
// maxCost is the maximum cost of an operation.
maxCost = Cost(math.MaxFloat64)
)
// Limit encapulates the configuration of a cost limit for an operation.
type Limit struct {
Threshold Cost
Enabled bool
}
// LimitManager manages configuration of a cost limit for an operation.
type LimitManager interface {
// Limit returns the current cost limit for an operation.
Limit() Limit
// Report reports metrics on the state of the manager.
Report()
// Close closes the manager.
Close()
}
// Tracker tracks the cost of operations seen so far.
type Tracker interface {
// Add adds c to the tracker's current cost total and returns the new total.
Add(c Cost) Cost
// Current returns the tracker's current cost total.
Current() Cost
}
// Enforcer instances enforce cost limits for operations.
type Enforcer interface {
Add(op Cost) Report
State() (Report, Limit)
Limit() Limit
Clone() Enforcer
Reporter() EnforcerReporter
}
// An EnforcerReporter is a listener for Enforcer events.
type EnforcerReporter interface {
// ReportCost is called on every call to Enforcer#Add with the added cost
ReportCost(c Cost)
// ReportCurrent reports the current total on every call to Enforcer#Add
ReportCurrent(c Cost)
// ReportOverLimit is called every time an enforcer goes over its limit. enabled is true if the limit manager
// says the limit is currently enabled.
ReportOverLimit(enabled bool)
}