-
Notifications
You must be signed in to change notification settings - Fork 2
/
circuit.go
54 lines (47 loc) · 1.2 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
package circuit
import "time"
const (
DefaultTimeout = 10 * time.Second
DefaultBaudRate = 250 * time.Millisecond
DefaultBackOff = time.Minute
DefaultWindow = 5 * time.Minute
minimumWindow = 5 * time.Second
minimumBackoff = time.Second
minimumBaudRate = 10 * time.Millisecond
)
// State represents the circuit breaker's state
type State uint32
const (
// Closed indicates that the circuit is
// functioning optimally
Closed State = iota
// Throttled indicates that the circuit
// breaker is recovering from an open state
Throttled
// Open indicates that the circuit breaker
// is currently open and rejecting all requests
Open
)
// String returns the state's string value
func (s State) String() string {
switch s {
case Closed:
return "closed"
case Throttled:
return "throttled"
case Open:
return "open"
}
return "unknown"
}
func (s State) MarshalJSON() ([]byte, error) {
switch s {
case Closed:
return []byte{'"', 'c', 'l', 'o', 's', 'e', 'd', '"'}, nil
case Throttled:
return []byte{'"', 't', 'h', 'r', 'o', 't', 't', 'l', 'e', 'd', '"'}, nil
case Open:
return []byte{'"', 'o', 'p', 'e', 'n', '"'}, nil
}
return []byte{'"', 'u', 'n', 'k', 'n', 'o', 'w', 'n', '"'}, nil
}