go-pattern-examples/resiliency/06_circuit_breaker/circuit_breaker.go

107 lines
2.7 KiB
Go
Raw 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-05-21 12:45:42 +03:00
* @Last Modified time: 2020-05-21 17:41:46
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 (
"errors"
2020-05-21 11:02:28 +03:00
"fmt"
2020-05-10 17:03:24 +03:00
"sync"
2020-05-08 10:51:33 +03:00
"time"
)
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-14 18:35:44 +03:00
2020-05-21 12:45:42 +03:00
//State 断路器本身的状态的
//State of switch int
2020-05-08 14:34:48 +03:00
type State int
2020-05-21 12:45:42 +03:00
// state for breaker
const (
StateClosed State = iota //默认的闭合状态,可以正常执行业务
StateHalfOpen
StateOpen
)
2020-05-21 11:02:28 +03:00
////////////////////////////////
//way 1 对象式断路器
////////////////////////////////
//RequestBreaker for protection
type RequestBreaker struct {
options Options
mutex sync.Mutex
2020-05-21 12:45:42 +03:00
state OperationState //断路器的当前状态
2020-05-21 11:02:28 +03:00
generation uint64
counts ICounter
}
//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,
WhenToBreak: func(counts counters) bool { return counts.ConsecutiveFailures > 2 },
OnStateChanged: func(name string, fromPre State, toCurrent State) {},
}
for _, setOption := range opts {
setOption(&defaultOptions)
}
return &RequestBreaker{
options: defaultOptions,
counts: &counters{},
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.
func (rb *RequestBreaker) Do(work func() (interface{}, error)) (interface{}, error) {
//before
fmt.Println("before do : request:", rb.counts.Total())
//do work from requested user
result, err := work()
fmt.Println("after do : request:", rb.counts.Total())
return result, err
}