mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
[WIP] circuit_breaker
This commit is contained in:
parent
78a2c3922b
commit
6cb54533ee
@ -8,6 +8,10 @@ Circuit Breaker Pattern 也叫断路器模式,断路器设计模式是故障
|
|||||||
|
|
||||||
![熔断器状态机](../../images/breaker-state-machine.png)
|
![熔断器状态机](../../images/breaker-state-machine.png)
|
||||||
|
|
||||||
|
状态变化流:
|
||||||
|
|
||||||
|
![状态变化流](../../images/breaker-state-machine-flow.png)
|
||||||
|
|
||||||
一些关键角色:
|
一些关键角色:
|
||||||
|
|
||||||
## Operation Counter 操作计数器
|
## Operation Counter 操作计数器
|
||||||
|
@ -19,22 +19,47 @@ func SetName(name string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetExpiry of breaker
|
//Interval of breaker
|
||||||
func SetExpiry(expiry time.Time) Option {
|
func Interval(interval time.Duration) Option {
|
||||||
|
return func(opts *Options) {
|
||||||
|
opts.Interval = interval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Timeout of breaker
|
||||||
|
func Timeout(timeout time.Duration) Option {
|
||||||
|
return func(opts *Options) {
|
||||||
|
opts.Timeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxRequests is the maximum number of requests allowed to pass through
|
||||||
|
// when the CircuitBreaker is half-open.
|
||||||
|
// If MaxRequests is 0, the CircuitBreaker allows only 1 request.
|
||||||
|
|
||||||
|
//MaxRequests of breaker
|
||||||
|
func MaxRequests(maxRequests uint32) Option {
|
||||||
|
return func(opts *Options) {
|
||||||
|
opts.MaxRequests = maxRequests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Expiry of breaker
|
||||||
|
func Expiry(expiry time.Time) Option {
|
||||||
return func(opts *Options) {
|
return func(opts *Options) {
|
||||||
opts.Expiry = expiry
|
opts.Expiry = expiry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetStateChangedHandle set handle of ChangedHandle
|
//OnStateChanged set handle of ChangedHandle
|
||||||
func SetStateChangedHandle(handler StateChangedEventHandler) Option {
|
func OnStateChanged(handler StateChangedEventHandler) Option {
|
||||||
return func(opts *Options) {
|
return func(opts *Options) {
|
||||||
opts.OnStateChanged = handler
|
opts.OnStateChanged = handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetReadyToTrip check traffic state ,to see if request can go
|
//ReadyToTrip check traffic state ,to see if request can go
|
||||||
func SetReadyToTrip(readyToGo StateCheckerHandler) Option {
|
func ReadyToTrip(readyToGo StateCheckerHandler) Option {
|
||||||
return func(opts *Options) {
|
return func(opts *Options) {
|
||||||
opts.ReadyToTrip = readyToGo
|
opts.ReadyToTrip = readyToGo
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ package circuit
|
|||||||
* @Author: Edward
|
* @Author: Edward
|
||||||
* @Date: 2020-05-10 22:00:58
|
* @Date: 2020-05-10 22:00:58
|
||||||
* @Last Modified by: Edward
|
* @Last Modified by: Edward
|
||||||
* @Last Modified time: 2020-05-11 11:57:21
|
* @Last Modified time: 2020-05-11 17:46:20
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -42,7 +42,7 @@ type RequestBreaker struct {
|
|||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
state State
|
state State
|
||||||
generation uint64
|
generation uint64
|
||||||
counts Counter
|
counts ICounter
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewRequestBreaker return a breaker
|
//NewRequestBreaker return a breaker
|
||||||
@ -52,7 +52,7 @@ func NewRequestBreaker(opts ...Option) *RequestBreaker {
|
|||||||
Name: "defaultBreakerName",
|
Name: "defaultBreakerName",
|
||||||
Expiry: time.Now().Add(time.Second * 20),
|
Expiry: time.Now().Add(time.Second * 20),
|
||||||
Interval: time.Second * 2,
|
Interval: time.Second * 2,
|
||||||
Timeout: time.Second * 5,
|
Timeout: time.Second * 60, //default to 60 seconds
|
||||||
MaxRequests: 5,
|
MaxRequests: 5,
|
||||||
ReadyToTrip: func(counts counters) bool { return true },
|
ReadyToTrip: func(counts counters) bool { return true },
|
||||||
OnStateChanged: func(name string, from State, to State) {},
|
OnStateChanged: func(name string, from State, to State) {},
|
||||||
@ -83,8 +83,8 @@ const (
|
|||||||
//Circuit of action stream
|
//Circuit of action stream
|
||||||
type Circuit func(context.Context) error
|
type Circuit func(context.Context) error
|
||||||
|
|
||||||
//Counter interface
|
//ICounter interface
|
||||||
type Counter interface {
|
type ICounter interface {
|
||||||
Count(State)
|
Count(State)
|
||||||
ConsecutiveFailures() uint32
|
ConsecutiveFailures() uint32
|
||||||
LastActivity() time.Time
|
LastActivity() time.Time
|
||||||
@ -92,6 +92,7 @@ type Counter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type counters struct {
|
type counters struct {
|
||||||
|
Requests uint32
|
||||||
state State
|
state State
|
||||||
lastActivity time.Time
|
lastActivity time.Time
|
||||||
counts uint32 //counts of failures
|
counts uint32 //counts of failures
|
||||||
@ -115,7 +116,7 @@ func (c *counters) Reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewCounter New Counter for Circuit Breaker
|
//NewCounter New Counter for Circuit Breaker
|
||||||
func NewCounter() Counter {
|
func NewCounter() ICounter {
|
||||||
return &counters{}
|
return &counters{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +128,7 @@ func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
|||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if cnt.ConsecutiveFailures() >= failureThreshold {
|
if cnt.ConsecutiveFailures() >= failureThreshold {
|
||||||
|
|
||||||
canRetry := func(cnt Counter) bool {
|
canRetry := func(cnt ICounter) bool {
|
||||||
backoffLevel := cnt.ConsecutiveFailures() - failureThreshold
|
backoffLevel := cnt.ConsecutiveFailures() - failureThreshold
|
||||||
|
|
||||||
// Calculates when should the circuit breaker resume propagating requests
|
// Calculates when should the circuit breaker resume propagating requests
|
||||||
|
Loading…
Reference in New Issue
Block a user