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 {
|
|
|
|
|
options Options
|
|
|
|
|
mutex sync.Mutex
|
2020-05-22 12:57:41 +03:00
|
|
|
|
state State
|
2020-05-21 11:02:28 +03:00
|
|
|
|
generation uint64
|
2020-06-02 18:55:59 +03:00
|
|
|
|
cnter counters
|
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),
|
|
|
|
|
Interval: time.Second * 2, // interval to check status
|
|
|
|
|
Timeout: time.Second * 60, //default to 60 seconds
|
|
|
|
|
MaxRequests: 5,
|
2020-05-22 12:57:41 +03:00
|
|
|
|
WhenToBreak: func(cnter counters) bool { return cnter.ConsecutiveFailures > 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{
|
|
|
|
|
options: defaultOptions,
|
2020-06-02 18:55:59 +03:00
|
|
|
|
cnter: counters{},
|
2020-05-21 11:02:28 +03:00
|
|
|
|
generation: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-06-02 18:55:59 +03:00
|
|
|
|
preState := StateUnknown
|
|
|
|
|
|
2020-05-21 11:02:28 +03:00
|
|
|
|
//before
|
2020-05-22 12:57:41 +03:00
|
|
|
|
fmt.Println("before do : request:", rb.cnter.Total())
|
2020-05-22 13:14:19 +03:00
|
|
|
|
rb.mutex.Lock()
|
2020-05-22 13:39:31 +03:00
|
|
|
|
|
2020-06-03 18:55:51 +03:00
|
|
|
|
//如果是断开状态,并且超时了,转到半开状态,记录
|
|
|
|
|
if rb.state == StateOpen && rb.options.Expiry.Before(time.Now()) {
|
|
|
|
|
preState = rb.state
|
|
|
|
|
rb.state = StateHalfOpen
|
|
|
|
|
rb.cnter.Reset()
|
|
|
|
|
} else {
|
|
|
|
|
return nil, ErrTooManyRequests
|
2020-05-22 13:39:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 07:29:45 +03:00
|
|
|
|
rb.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
//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-02 18:55:59 +03:00
|
|
|
|
rb.mutex.Lock()
|
|
|
|
|
//失败了
|
2020-05-22 13:14:19 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
rb.cnter.Count(FailureState)
|
2020-06-02 18:55:59 +03:00
|
|
|
|
//如果是在半开状态下的失败,立即打开开关
|
|
|
|
|
if rb.state == StateHalfOpen {
|
2020-06-03 18:55:51 +03:00
|
|
|
|
rb.state = StateOpen //转为打开
|
|
|
|
|
rb.options.OnStateChanged(rb.options.Name, StateHalfOpen, StateOpen) //半开到打开
|
2020-06-02 18:55:59 +03:00
|
|
|
|
} else if rb.state == StateClosed {
|
|
|
|
|
if rb.options.WhenToBreak(rb.cnter) {
|
2020-06-03 18:55:51 +03:00
|
|
|
|
rb.state = StateOpen //打开开关
|
|
|
|
|
rb.options.OnStateChanged(rb.options.Name, StateClosed, StateOpen) //关闭到打开
|
2020-06-02 18:55:59 +03:00
|
|
|
|
rb.cnter.Reset()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 13:14:19 +03:00
|
|
|
|
} else {
|
2020-06-03 18:55:51 +03:00
|
|
|
|
//成功了.
|
2020-05-22 13:14:19 +03:00
|
|
|
|
rb.cnter.Count(SuccessState)
|
2020-06-03 18:55:51 +03:00
|
|
|
|
if rb.state == StateHalfOpen {
|
|
|
|
|
if preState == StateOpen {
|
|
|
|
|
preState = StateHalfOpen
|
|
|
|
|
rb.options.OnStateChanged(rb.options.Name, StateOpen, StateHalfOpen) //打开到半开
|
|
|
|
|
}
|
|
|
|
|
if rb.cnter.ConsecutiveSuccesses >= rb.options.ShoulderToOpen {
|
|
|
|
|
rb.state = StateClosed
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
rb.mutex.Unlock()
|
|
|
|
|
//after
|
2020-05-22 12:57:41 +03:00
|
|
|
|
fmt.Println("after do : request:", rb.cnter.Total())
|
2020-05-21 11:02:28 +03:00
|
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
|
}
|