-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripper.go
219 lines (190 loc) · 6.69 KB
/
tripper.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package tripper
import (
"fmt"
"sync"
"time"
)
// threshold type can be only COUNT or PERCENTAGE
// ThresholdCount represents a threshold type based on count.
const (
ThresholdCount = "COUNT"
ThresholdPercentage = "PERCENTAGE"
ThresholdConsecutive = "CONSECUTIVE"
)
var thresholdTypes = []string{ThresholdCount, ThresholdPercentage, ThresholdConsecutive}
// Circuit represents a monitoring entity that tracks the status of a circuit.
type Circuit interface {
UpdateStatus(success bool)
IsCircuitOpen() bool
Data() CircuitData
}
// CircuitOptions represents options for configuring a Circuit.
type CircuitOptions struct {
Name string // Name of the circuit
Threshold float32 // Threshold value for triggering circuit open
ThresholdType string // Type of threshold (e.g., percentage, count)
MinimumCount int64 // Minimum number of events required for monitoring
IntervalInSeconds int // Interval in seconds for monitoring (should be non-zero and multiple of 60)
OnCircuitOpen func(t CallbackEvent)
OnCircuitClosed func(t CallbackEvent)
}
type CircuitData struct {
SuccessCount int64
FailureCount int64
IsCircuitOpen bool
CircuitOpenedSince int64
}
// CircuitImplementation represents the implementation of the Circuit interface.
type CircuitImplementation struct {
Options CircuitOptions
FailureCount int64 // Number of failures recorded
SuccessCount int64 // Number of successes recorded
CircuitOpen bool // Indicates whether the circuit is open or closed
LastCapturedAt int64 // Timestamp of the last captured event
CircuitOpenedSince int64 // Timestamp when the circuit was opened
ConsecutiveCounter int64
Ticker *time.Ticker
Mutex sync.Mutex
XMutex sync.Mutex
}
// CallbackEvent represents an event callback for the circuit.
type CallbackEvent struct {
Timestamp int64
SuccessCount int64
FailureCount int64
}
func (m *CircuitImplementation) Data() CircuitData {
return CircuitData{
SuccessCount: m.SuccessCount,
FailureCount: m.FailureCount,
IsCircuitOpen: m.CircuitOpen,
CircuitOpenedSince: m.CircuitOpenedSince,
}
}
// ConfigureCircuit creates and configures a new Circuit with the provided options.
func ConfigureCircuit(monitorOptions CircuitOptions) (Circuit, error) {
validThresholdType := false
for _, thType := range thresholdTypes {
if thType == monitorOptions.ThresholdType {
validThresholdType = true
break
}
}
if !validThresholdType {
return nil, fmt.Errorf("invalid threshold type %s", monitorOptions.ThresholdType)
}
//if the threshold type is percentage, check if the threshold is between 0 and 100
if monitorOptions.ThresholdType == ThresholdPercentage && (monitorOptions.Threshold < 0 || monitorOptions.Threshold > 100) {
return nil, fmt.Errorf("invalid threshold value %f for percentage type", monitorOptions.Threshold)
}
// if the threshold type is count, check if the threshold is greater than 0
if monitorOptions.ThresholdType == ThresholdCount && monitorOptions.Threshold <= 0 {
return nil, fmt.Errorf("invalid threshold value %f for count type", monitorOptions.Threshold)
}
// if the minimum count is less than 1, return an error
if monitorOptions.MinimumCount < 1 {
return nil, fmt.Errorf("invalid minimum count %d", monitorOptions.MinimumCount)
}
//if threshold is type count then minimum count should be greater than threshold
if monitorOptions.ThresholdType == ThresholdCount && monitorOptions.MinimumCount <= int64(monitorOptions.Threshold) {
return nil, fmt.Errorf("minimum count should be greater than threshold")
}
// if the interval is less than 5, return an error
if monitorOptions.IntervalInSeconds < 5 {
return nil, fmt.Errorf("invalid interval %d", monitorOptions.IntervalInSeconds)
}
newMonitor := &CircuitImplementation{
Options: monitorOptions,
ConsecutiveCounter: 0,
}
newMonitor.Ticker = time.NewTicker(time.Duration(monitorOptions.IntervalInSeconds) * time.Second)
go func() {
for range newMonitor.Ticker.C {
newMonitor.SuccessCount = 0
newMonitor.FailureCount = 0
newMonitor.CircuitOpenedSince = 0
newMonitor.ConsecutiveCounter = 0
newMonitor.CircuitOpen = false
if newMonitor.Options.OnCircuitClosed != nil {
newMonitor.Options.OnCircuitClosed(CallbackEvent{
Timestamp: getTimestamp(),
SuccessCount: newMonitor.SuccessCount,
FailureCount: newMonitor.FailureCount,
})
}
}
}()
return newMonitor, nil
}
// UpdateStatus updates the status of the Circuit based on the success of the event.
func (m *CircuitImplementation) UpdateStatus(success bool) {
//add a lock here
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.LastCapturedAt = getTimestamp()
if success {
m.ConsecutiveCounter = 0
m.SuccessCount++
} else {
m.ConsecutiveCounter++
m.FailureCount++
}
if m.SuccessCount+m.FailureCount < m.Options.MinimumCount {
return
}
currentStateOfCircuit := m.CircuitOpen
if m.Options.ThresholdType == ThresholdCount {
if float32(m.FailureCount) >= m.Options.Threshold {
m.CircuitOpen = true
m.CircuitOpenedSince = m.LastCapturedAt
} else {
m.CircuitOpen = false
m.CircuitOpenedSince = 0
}
} else if m.Options.ThresholdType == ThresholdPercentage {
// if the threshold type is percentage, check if the percentage of failures is greater than the threshold
totalRequests := m.FailureCount + m.SuccessCount
failurePercentage := (m.FailureCount * 100) / totalRequests
if float32(failurePercentage) >= m.Options.Threshold {
m.CircuitOpen = true
m.CircuitOpenedSince = m.LastCapturedAt
} else {
m.CircuitOpen = false
m.CircuitOpenedSince = 0
}
} else if m.Options.ThresholdType == ThresholdConsecutive {
if m.ConsecutiveCounter >= int64(m.Options.Threshold) {
m.CircuitOpen = true
m.CircuitOpenedSince = m.LastCapturedAt
} else {
m.CircuitOpen = false
m.CircuitOpenedSince = 0
}
}
if currentStateOfCircuit != m.CircuitOpen && m.CircuitOpen {
if m.Options.OnCircuitOpen != nil {
m.Options.OnCircuitOpen(CallbackEvent{
Timestamp: m.LastCapturedAt,
SuccessCount: m.SuccessCount,
FailureCount: m.FailureCount,
})
}
} else if currentStateOfCircuit != m.CircuitOpen && !m.CircuitOpen {
if m.Options.OnCircuitClosed != nil {
m.Options.OnCircuitClosed(CallbackEvent{
Timestamp: m.LastCapturedAt,
SuccessCount: m.SuccessCount,
FailureCount: m.FailureCount,
})
}
}
}
// IsCircuitOpen returns true if the circuit is open, false otherwise.
func (m *CircuitImplementation) IsCircuitOpen() bool {
return m.CircuitOpen
}
// getTimestamp returns the current timestamp in Unix format.
func getTimestamp() int64 {
currentTime := time.Now()
return currentTime.Unix()
}