2020-05-22 08:21:54 +03:00
|
|
|
|
/*
|
|
|
|
|
* @Description: https://github.com/crazybber
|
|
|
|
|
* @Author: Edward
|
|
|
|
|
* @Date: 2020-05-22 12:42:34
|
|
|
|
|
* @Last Modified by: Edward
|
2020-05-22 12:57:41 +03:00
|
|
|
|
* @Last Modified time: 2020-05-22 17:21:40
|
2020-05-22 08:21:54 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package circuit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
////////////////////////////////
|
2020-06-02 18:55:42 +03:00
|
|
|
|
//way 1 简单的函数式断路器
|
2020-05-22 08:21:54 +03:00
|
|
|
|
// 一个func实例作为一个Breaker 允许多个worker(即goroutine)同时访问
|
2020-05-22 11:50:41 +03:00
|
|
|
|
// 当前简单场景下,只考虑单个worker情况下的连续请求
|
2020-05-22 10:31:28 +03:00
|
|
|
|
// 当前的设计思路,非常简单:
|
|
|
|
|
// 1、不考虑三种状态之间的转换,只靠两种状态,电路打开与电路关闭
|
|
|
|
|
// 2、当累计失败到达一定失败次数就端开请求(打开电路),并延迟一定的时间再允许请求
|
2020-05-22 08:21:54 +03:00
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
2020-05-22 10:31:28 +03:00
|
|
|
|
//states of CircuitBreaker
|
|
|
|
|
//states: closed --->open ---> half open --> closed
|
|
|
|
|
const (
|
|
|
|
|
UnknownState OperationState = iota
|
|
|
|
|
FailureState
|
|
|
|
|
SuccessState
|
|
|
|
|
)
|
|
|
|
|
|
2020-05-22 11:50:41 +03:00
|
|
|
|
type simpleCounter struct {
|
2020-05-22 12:57:41 +03:00
|
|
|
|
lastOpResult OperationState
|
2020-05-22 11:50:41 +03:00
|
|
|
|
lastActivity time.Time
|
|
|
|
|
ConsecutiveSuccesses uint32
|
|
|
|
|
ConsecutiveFailures uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *simpleCounter) LastActivity() time.Time {
|
|
|
|
|
return c.lastActivity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *simpleCounter) Reset() {
|
|
|
|
|
ct := &simpleCounter{}
|
|
|
|
|
ct.lastActivity = c.lastActivity
|
2020-05-22 12:57:41 +03:00
|
|
|
|
ct.lastOpResult = UnknownState
|
2020-05-22 11:50:41 +03:00
|
|
|
|
c = ct
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Count the failure and success
|
2020-05-22 12:57:41 +03:00
|
|
|
|
func (c *simpleCounter) Count(lastState OperationState) {
|
2020-05-22 11:50:41 +03:00
|
|
|
|
|
2020-05-22 12:57:41 +03:00
|
|
|
|
switch lastState {
|
2020-05-22 11:50:41 +03:00
|
|
|
|
case FailureState:
|
|
|
|
|
c.ConsecutiveFailures++
|
|
|
|
|
case SuccessState:
|
|
|
|
|
c.ConsecutiveSuccesses++
|
|
|
|
|
}
|
|
|
|
|
c.lastActivity = time.Now() //更新活动时间
|
2020-05-22 12:57:41 +03:00
|
|
|
|
c.lastOpResult = lastState
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//handle status change
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 08:21:54 +03:00
|
|
|
|
//Circuit of action stream,this is actually to do something.
|
|
|
|
|
//Circuit hold the really action
|
|
|
|
|
type Circuit func(context.Context) error
|
|
|
|
|
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//失败达到阈值后,过两秒重试
|
|
|
|
|
var canRetry = func(cnt simpleCounter, failureThreshold uint32) bool {
|
2020-05-22 10:31:28 +03:00
|
|
|
|
backoffLevel := cnt.ConsecutiveFailures - failureThreshold
|
|
|
|
|
// Calculates when should the circuit breaker resume propagating requests
|
|
|
|
|
// to the service
|
|
|
|
|
shouldRetryAt := cnt.LastActivity().Add(time.Second << backoffLevel)
|
|
|
|
|
return time.Now().After(shouldRetryAt)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//Breaker return a closure wrapper to hold Circuit Request
|
2020-05-22 08:21:54 +03:00
|
|
|
|
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
|
|
|
|
|
2020-05-22 10:31:28 +03:00
|
|
|
|
//闭包内部的全局计数器 和状态标志
|
2020-05-22 11:50:41 +03:00
|
|
|
|
cnt := simpleCounter{}
|
2020-05-22 08:21:54 +03:00
|
|
|
|
|
|
|
|
|
//ctx can be used hold parameters
|
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
|
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//阻止请求
|
2020-05-22 08:21:54 +03:00
|
|
|
|
if cnt.ConsecutiveFailures >= failureThreshold {
|
2020-05-22 10:31:28 +03:00
|
|
|
|
if !canRetry(cnt, failureThreshold) {
|
2020-05-22 08:21:54 +03:00
|
|
|
|
// Fails fast instead of propagating requests to the circuit since
|
|
|
|
|
// not enough time has passed since the last failure to retry
|
|
|
|
|
return ErrServiceUnavailable
|
|
|
|
|
}
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//reset mark for failures
|
|
|
|
|
cnt.ConsecutiveFailures = 0
|
2020-05-22 08:21:54 +03:00
|
|
|
|
}
|
2020-05-22 11:50:41 +03:00
|
|
|
|
|
2020-05-22 08:21:54 +03:00
|
|
|
|
// Unless the failure threshold is exceeded the wrapped service mimics the
|
|
|
|
|
// old behavior and the difference in behavior is seen after consecutive failures
|
2020-05-22 10:31:28 +03:00
|
|
|
|
if err := c(ctx); err != nil {
|
2020-05-22 11:50:41 +03:00
|
|
|
|
//连续失败会增大backoff 时间
|
2020-05-22 10:31:28 +03:00
|
|
|
|
cnt.Count(FailureState)
|
|
|
|
|
return err
|
2020-05-22 08:21:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 10:31:28 +03:00
|
|
|
|
cnt.Count(SuccessState)
|
2020-05-22 08:21:54 +03:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|