|
| 1 | +// Package circuitbreaker implements the circuit breaker. |
| 2 | +// |
| 3 | +// Sentinel circuit breaker module converts each Rule into a CircuitBreaker. Each CircuitBreaker has its own statistical structure. |
| 4 | +// |
| 5 | +// Sentinel circuit breaker module supports three strategies: |
| 6 | +// |
| 7 | +// 1. SlowRequestRatio: the ratio of slow response time entry(entry's response time is great than max slow response time) exceeds the threshold. The following entry to resource will be broken. |
| 8 | +// In SlowRequestRatio strategy, user must set max response time. |
| 9 | +// 2. ErrorRatio: the ratio of error entry exceeds the threshold. The following entry to resource will be broken. |
| 10 | +// 3. ErrorCount: the number of error entry exceeds the threshold. The following entry to resource will be broken. |
| 11 | +// |
| 12 | +// Sentinel circuit breaker is implemented based on state machines. There are three state: |
| 13 | +// |
| 14 | +// 1. Closed: all entries could pass checking. |
| 15 | +// 2. Open: the circuit breaker is broken, all entries are blocked. After retry timeout, circuit breaker switches state to Half-Open and allows one entry to probe whether the resource returns to its expected state. |
| 16 | +// 3. Half-Open: the circuit breaker is in a temporary state of probing, only one entry is allowed to access resource, others are blocked. |
| 17 | +// |
| 18 | +// Sentinel circuit breaker provides the listener to listen on the state changes. |
| 19 | +// |
| 20 | +// type StateChangeListener interface { |
| 21 | +// OnTransformToClosed(prev State, rule Rule) |
| 22 | +// |
| 23 | +// OnTransformToOpen(prev State, rule Rule, snapshot interface{}) |
| 24 | +// |
| 25 | +// OnTransformToHalfOpen(prev State, rule Rule) |
| 26 | +// } |
| 27 | +// |
| 28 | +// Here is the example code to use circuit breaker: |
| 29 | +// |
| 30 | +// type stateChangeTestListener struct {} |
| 31 | +// |
| 32 | +// func (s *stateChangeTestListener) OnTransformToClosed(prev circuitbreaker.State, rule circuitbreaker.Rule) { |
| 33 | +// fmt.Printf("rule.steategy: %+v, From %s to Closed, time: %d\n", rule.Strategy, prev.String(), util.CurrentTimeMillis()) |
| 34 | +// } |
| 35 | +// |
| 36 | +// func (s *stateChangeTestListener) OnTransformToOpen(prev circuitbreaker.State, rule circuitbreaker.Rule, snapshot interface{}) { |
| 37 | +// fmt.Printf("rule.steategy: %+v, From %s to Open, snapshot: %.2f, time: %d\n", rule.Strategy, prev.String(), snapshot, util.CurrentTimeMillis()) |
| 38 | +// } |
| 39 | +// |
| 40 | +// func (s *stateChangeTestListener) OnTransformToHalfOpen(prev circuitbreaker.State, rule circuitbreaker.Rule) { |
| 41 | +// fmt.Printf("rule.steategy: %+v, From %s to Half-Open, time: %d\n", rule.Strategy, prev.String(), util.CurrentTimeMillis()) |
| 42 | +// } |
| 43 | +// |
| 44 | +// func main() { |
| 45 | +// err := sentinel.InitDefault() |
| 46 | +// if err != nil { |
| 47 | +// log.Fatal(err) |
| 48 | +// } |
| 49 | +// ch := make(chan struct{}) |
| 50 | +// // Register a state change listener so that we could observer the state change of the internal circuit breaker. |
| 51 | +// circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{}) |
| 52 | +// |
| 53 | +// _, err = circuitbreaker.LoadRules([]*circuitbreaker.Rule{ |
| 54 | +// // Statistic time span=10s, recoveryTimeout=3s, slowRtUpperBound=50ms, maxSlowRequestRatio=50% |
| 55 | +// { |
| 56 | +// Resource: "abc", |
| 57 | +// Strategy: circuitbreaker.SlowRequestRatio, |
| 58 | +// RetryTimeoutMs: 3000, |
| 59 | +// MinRequestAmount: 10, |
| 60 | +// StatIntervalMs: 10000, |
| 61 | +// MaxAllowedRtMs: 50, |
| 62 | +// Threshold: 0.5, |
| 63 | +// }, |
| 64 | +// // Statistic time span=10s, recoveryTimeout=3s, maxErrorRatio=50% |
| 65 | +// { |
| 66 | +// Resource: "abc", |
| 67 | +// Strategy: circuitbreaker.ErrorRatio, |
| 68 | +// RetryTimeoutMs: 3000, |
| 69 | +// MinRequestAmount: 10, |
| 70 | +// StatIntervalMs: 10000, |
| 71 | +// Threshold: 0.5, |
| 72 | +// }, |
| 73 | +// }) |
| 74 | +// if err != nil { |
| 75 | +// log.Fatal(err) |
| 76 | +// } |
| 77 | +// |
| 78 | +// fmt.Println("Sentinel Go circuit breaking demo is running. You may see the pass/block metric in the metric log.") |
| 79 | +// go func() { |
| 80 | +// for { |
| 81 | +// e, b := sentinel.Entry("abc") |
| 82 | +// if b != nil { |
| 83 | +// //fmt.Println("g1blocked") |
| 84 | +// time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) |
| 85 | +// } else { |
| 86 | +// if rand.Uint64()%20 > 9 { |
| 87 | +// // Record current invocation as error. |
| 88 | +// sentinel.TraceError(e, errors.New("biz error")) |
| 89 | +// } |
| 90 | +// //fmt.Println("g1passed") |
| 91 | +// time.Sleep(time.Duration(rand.Uint64()%80+10) * time.Millisecond) |
| 92 | +// e.Exit() |
| 93 | +// } |
| 94 | +// } |
| 95 | +// }() |
| 96 | +// |
| 97 | +// go func() { |
| 98 | +// for { |
| 99 | +// e, b := sentinel.Entry("abc") |
| 100 | +// if b != nil { |
| 101 | +// //fmt.Println("g2blocked") |
| 102 | +// time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) |
| 103 | +// } else { |
| 104 | +// //fmt.Println("g2passed") |
| 105 | +// time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond) |
| 106 | +// e.Exit() |
| 107 | +// } |
| 108 | +// } |
| 109 | +// }() |
| 110 | +// <-ch |
| 111 | +// } |
| 112 | +// |
| 113 | +package circuitbreaker |
0 commit comments