-
Notifications
You must be signed in to change notification settings - Fork 4
/
circuit.go
176 lines (139 loc) · 4.4 KB
/
circuit.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
package goHystrix
import (
"bytes"
"fmt"
"sync"
)
type CircuitBreaker struct {
name string
group string
metric *Metric
errorsThreshold float64
minRequestThreshold int64
}
var (
circuits = NewCircuitsHolder()
)
type CircuitHolder struct {
circuits map[string]map[string]*CircuitBreaker
mutex sync.RWMutex
}
func NewCircuitNoParams(group string, name string) *CircuitBreaker {
return NewCircuit(group, name, CommandOptionsDefaults())
}
func NewCircuit(group string, name string, options CommandOptions) *CircuitBreaker {
c, ok := Circuits().Get(group, name)
if ok {
return c
}
metric := NewMetricWithParams(group, name, options.NumberOfSecondsToStore, options.NumberOfSamplesToStore)
c = &CircuitBreaker{
name: name,
group: group,
metric: metric,
errorsThreshold: options.ErrorsThreshold,
minRequestThreshold: options.MinimumNumberOfRequest,
}
Circuits().Set(group, name, c)
return c
}
func (c *CircuitBreaker) IsOpen() (bool, string) {
counts := c.metric.HealthCounts()
if counts.Total < c.minRequestThreshold {
return false, "CLOSE: not enought request"
}
if counts.ErrorPercentage >= c.errorsThreshold {
return true, "OPEN: to many errors"
}
return false, "CLOSE: all ok"
}
func (c *CircuitBreaker) Metric() *Metric {
return c.metric
}
func (c *CircuitBreaker) ToJSON() string {
var buffer bytes.Buffer
buffer.WriteString("{\n")
open, state := c.IsOpen()
counts := c.Metric().doHealthCounts()
stats := c.Metric().Stats()
lastSuccess := c.Metric().LastSuccess()
lastFailure := c.Metric().LastFailure()
lastTimeout := c.Metric().LastTimeout()
fmt.Fprintf(&buffer, "\"name\" : \"%s\",\n", c.name)
fmt.Fprintf(&buffer, "\"group\" : \"%s\",\n", c.group)
fmt.Fprintf(&buffer, "\"isOpen\" : \"%t\",\n", open)
fmt.Fprintf(&buffer, "\"state\" : \"%s\",\n", state)
fmt.Fprintf(&buffer, "\"percentile90\" : \"%f\",\n", stats.Percentile(0.90))
fmt.Fprintf(&buffer, "\"mean\" : \"%f\",\n", stats.Mean())
fmt.Fprintf(&buffer, "\"variance\" : \"%f\",\n", stats.Variance())
fmt.Fprintf(&buffer, "\"max\" : \"%d\",\n", stats.Max())
fmt.Fprintf(&buffer, "\"min\" : \"%d\",\n", stats.Min())
fmt.Fprintf(&buffer, "\"failures\" : \"%d\",\n", counts.Failures)
fmt.Fprintf(&buffer, "\"timeouts\" : \"%d\",\n", counts.Timeouts)
fmt.Fprintf(&buffer, "\"fallback\" : \"%d\",\n", counts.Fallback)
fmt.Fprintf(&buffer, "\"panics\" : \"%d\",\n", counts.Panics)
fmt.Fprintf(&buffer, "\"fallbackErrors\" : \"%d\",\n", counts.FallbackErrors)
fmt.Fprintf(&buffer, "\"total\" : \"%d\",\n", counts.Total)
fmt.Fprintf(&buffer, "\"success\" : \"%d\",\n", counts.Success)
fmt.Fprintf(&buffer, "\"errorPercentage\" : \"%f\",\n", counts.ErrorPercentage)
fmt.Fprintf(&buffer, "\"lastSuccess\" : \"%s\",\n", lastSuccess)
fmt.Fprintf(&buffer, "\"lastFailure\" : \"%s\",\n", lastFailure)
fmt.Fprintf(&buffer, "\"lastTimeout\" : \"%s\"\n", lastTimeout)
buffer.WriteString("\n}")
return buffer.String()
}
func NewCircuitsHolder() *CircuitHolder {
return &CircuitHolder{circuits: make(map[string]map[string]*CircuitBreaker)}
}
func Circuits() *CircuitHolder {
return circuits
}
func CircuitsReset() {
circuits = NewCircuitsHolder()
}
func (holder *CircuitHolder) Get(group string, name string) (*CircuitBreaker, bool) {
holder.mutex.RLock()
defer holder.mutex.RUnlock()
circuitsValues, ok := holder.circuits[group]
if !ok {
return nil, ok
}
value, ok := circuitsValues[name]
return value, ok
}
func (holder *CircuitHolder) Set(group string, name string, value *CircuitBreaker) {
holder.mutex.Lock()
defer holder.mutex.Unlock()
circuitsValues, ok := holder.circuits[group]
if !ok {
circuitsValues = make(map[string]*CircuitBreaker)
holder.circuits[group] = circuitsValues
}
circuitsValues[name] = value
}
func (holder *CircuitHolder) ToJSON() string {
holder.mutex.RLock()
defer holder.mutex.RUnlock()
var buffer bytes.Buffer
buffer.WriteString("[\n")
first := true
for group, names := range holder.circuits {
if !first {
fmt.Fprintf(&buffer, ",\n")
}
first = false
nested_first := true
fmt.Fprintf(&buffer, "{\"group\" : \"%s\",\n", group)
fmt.Fprintf(&buffer, "\"circuit\" : [\n")
for _, circuit := range names {
if !nested_first {
fmt.Fprintf(&buffer, ",\n")
}
nested_first = false
buffer.WriteString(circuit.ToJSON())
}
fmt.Fprintf(&buffer, "] }\n")
}
buffer.WriteString("\n]")
return buffer.String()
}