go-pattern-examples/resiliency/01_circuit_breaker/circuit_breaker_adv.go

171 lines
4.4 KiB
Go
Raw Permalink Normal View History

2020-05-10 17:03:24 +03:00
/*
* @Description: https://github.com/crazybber
* @Author: Edward
* @Date: 2020-05-10 22:00:58
* @Last Modified by: Edward
2020-06-03 18:55:51 +03:00
* @Last Modified time: 2020-06-03 23:54:29
2020-05-10 17:03:24 +03:00
*/
2020-05-21 12:45:42 +03:00
package circuit
2020-05-08 10:51:33 +03:00
import (
2020-05-22 13:14:19 +03:00
"context"
2020-05-22 13:39:31 +03:00
"errors"
2020-05-21 11:02:28 +03:00
"fmt"
2020-05-10 17:03:24 +03:00
"sync"
2020-05-22 13:39:31 +03:00
"time"
2020-05-08 10:51:33 +03:00
)
2020-05-10 17:03:24 +03:00
////////////////////////////////
///使用HTTP请求的例子
//每个搜索引擎时时刻刻都会遇到超大规模的请求的流量.
//这里演示一个复杂一点的例子同时使用Option 模式
2020-05-08 10:51:33 +03:00
//ErrServiceUnavailable for error
var (
2020-05-08 14:34:48 +03:00
ErrTooManyRequests = errors.New("too many requests")
ErrServiceUnavailable = errors.New("service unavailable")
2020-05-21 11:02:28 +03:00
FailureThreshold = 10 //最大失败次数--->失败阈值
2020-05-08 10:51:33 +03:00
)
2020-05-21 12:45:42 +03:00
// 默认的超时时间
2020-05-22 08:21:54 +03:00
const (
defaultTimeout = time.Second * 30
defaultSuccessThreshold = 2
)
2020-05-21 12:45:42 +03:00
2020-05-21 11:02:28 +03:00
////////////////////////////////
2020-05-22 11:50:41 +03:00
//way 2 对象式断路器
// 高级模式
2020-05-22 12:57:41 +03:00
// 支持多工作者模式
2020-05-21 11:02:28 +03:00
////////////////////////////////
//RequestBreaker for protection
type RequestBreaker struct {
2020-06-04 12:04:53 +03:00
options Options
mutex sync.Mutex
state State
cnter counters
preState State
2020-05-21 11:02:28 +03:00
}
//NewRequestBreaker return a breaker
func NewRequestBreaker(opts ...Option) *RequestBreaker {
defaultOptions := Options{
Name: "defaultBreakerName",
Expiry: time.Now().Add(time.Second * 20),
2020-06-04 12:04:53 +03:00
Interval: time.Second * 10, // interval to check closed status,default 10 seconds
Timeout: time.Second * 60, //timeout to check open, default 60 seconds
2020-05-21 11:02:28 +03:00
MaxRequests: 5,
2020-06-04 12:04:53 +03:00
CanOpen: func(current State, cnter counters) bool { return cnter.ConsecutiveFailures > 2 },
CanClose: func(current State, cnter counters) bool { return cnter.ConsecutiveSuccesses > 2 },
2020-05-21 11:02:28 +03:00
OnStateChanged: func(name string, fromPre State, toCurrent State) {},
}
for _, setOption := range opts {
setOption(&defaultOptions)
}
return &RequestBreaker{
2020-06-04 12:04:53 +03:00
options: defaultOptions,
cnter: counters{},
state: StateUnknown,
preState: StateUnknown,
2020-05-21 11:02:28 +03:00
}
}
2020-06-04 12:04:53 +03:00
func (rb *RequestBreaker) changeStateTo(state State) {
rb.preState = rb.state
rb.state = state
rb.cnter.Reset()
}
func (rb *RequestBreaker) beforeRequest() error {
rb.mutex.Lock()
defer rb.mutex.Unlock()
fmt.Println("before do request:", rb.cnter.Total())
switch rb.state {
case StateOpen:
//如果是断开状态,并且超时了,转到半开状态,记录
if rb.options.Expiry.Before(time.Now()) {
rb.changeStateTo(StateHalfOpen)
rb.options.Expiry = time.Now().Add(rb.options.Timeout)
return nil
}
2020-06-04 12:39:45 +03:00
return ErrTooManyRequests
2020-06-04 12:04:53 +03:00
case StateClosed:
if rb.options.Expiry.Before(time.Now()) {
rb.cnter.Reset()
rb.options.Expiry = time.Now().Add(rb.options.Interval)
}
}
2020-06-04 12:39:45 +03:00
return nil
2020-06-04 12:04:53 +03:00
}
2020-05-21 11:02:28 +03:00
// Do the given requested work if the RequestBreaker accepts it.
// Do returns an error instantly if the RequestBreaker rejects the request.
// Otherwise, Execute returns the result of the request.
// If a panic occurs in the request, the RequestBreaker handles it as an error and causes the same panic again.
2020-05-22 13:14:19 +03:00
func (rb *RequestBreaker) Do(work func(ctx context.Context) (interface{}, error)) (interface{}, error) {
2020-05-21 11:02:28 +03:00
//before
2020-05-22 13:39:31 +03:00
2020-06-04 12:04:53 +03:00
if err := rb.beforeRequest(); err != nil {
return nil, err
2020-05-22 13:39:31 +03:00
}
2020-05-28 07:29:45 +03:00
//do work
2020-05-21 11:02:28 +03:00
//do work from requested user
2020-05-22 13:14:19 +03:00
result, err := work(rb.options.Ctx)
2020-06-04 12:04:53 +03:00
//after work
rb.afterRequest(err)
return result, err
}
func (rb *RequestBreaker) afterRequest(resultErr error) {
2020-06-02 18:55:59 +03:00
rb.mutex.Lock()
2020-06-04 12:04:53 +03:00
defer rb.mutex.Unlock()
//after
fmt.Println("after do request:", rb.cnter.Total())
if resultErr != nil {
//失败了,handle 失败
rb.cnter.Count(FailureState, rb.preState == rb.state)
switch rb.state {
case StateHalfOpen, StateClosed:
if rb.options.CanOpen(rb.state, rb.cnter) {
rb.changeStateTo(StateOpen) //打开开关
rb.options.OnStateChanged(rb.options.Name, rb.state, StateOpen) //关闭到打开
2020-06-02 18:55:59 +03:00
}
}
2020-05-22 13:14:19 +03:00
} else {
2020-06-04 12:04:53 +03:00
//success !
rb.cnter.Count(SuccessState, rb.preState == rb.state)
switch rb.state {
case StateHalfOpen:
if rb.preState == StateOpen {
// rb.changeStateTo(StateHalfOpen) //already handled in beforeRequest,Only fire StateChange Event
2020-06-03 18:55:51 +03:00
rb.options.OnStateChanged(rb.options.Name, StateOpen, StateHalfOpen) //打开到半开
}
2020-06-04 12:04:53 +03:00
if rb.cnter.ConsecutiveSuccesses >= rb.options.ShoulderHalfToOpen {
rb.changeStateTo(StateClosed)
rb.options.Expiry = time.Now().Add(rb.options.Interval)
2020-06-03 18:55:51 +03:00
rb.options.OnStateChanged(rb.options.Name, StateHalfOpen, StateClosed) //半开到关闭
}
2020-06-02 18:55:59 +03:00
}
2020-05-21 11:02:28 +03:00
2020-06-02 18:55:59 +03:00
}
2020-05-21 11:02:28 +03:00
}