mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2025-02-18 02:03:12 +03:00
[WIP] circuit_breaker
This commit is contained in:
parent
8343f7c91d
commit
94b8f71cc6
@ -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 21:37:13
|
* @Last Modified time: 2020-05-11 22:15:25
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -86,26 +86,19 @@ type Circuit func(context.Context) error
|
|||||||
//ICounter interface
|
//ICounter interface
|
||||||
type ICounter interface {
|
type ICounter interface {
|
||||||
Count(State)
|
Count(State)
|
||||||
ConsecutiveFailures() uint32
|
|
||||||
LastActivity() time.Time
|
LastActivity() time.Time
|
||||||
Reset()
|
Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
type counters struct {
|
type counters struct {
|
||||||
TotalFailures, Requests uint32
|
Requests uint32
|
||||||
state State
|
lastState State
|
||||||
lastActivity time.Time
|
lastActivity time.Time
|
||||||
counts uint32 //counts of failures
|
counts uint32 //counts of failures
|
||||||
|
TotalFailures uint32
|
||||||
}
|
TotalSuccesses uint32
|
||||||
|
ConsecutiveSuccesses uint32
|
||||||
func (c *counters) Count(state State) {
|
ConsecutiveFailures uint32
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *counters) ConsecutiveFailures() uint32 {
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *counters) LastActivity() time.Time {
|
func (c *counters) LastActivity() time.Time {
|
||||||
@ -116,22 +109,31 @@ func (c *counters) Reset() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewCounter New Counter for Circuit Breaker
|
func (c *counters) Count(statue State) {
|
||||||
func NewCounter() ICounter {
|
|
||||||
return &counters{}
|
switch statue {
|
||||||
|
case FailureState:
|
||||||
|
c.ConsecutiveFailures++
|
||||||
|
case SuccessState:
|
||||||
|
c.ConsecutiveSuccesses++
|
||||||
|
}
|
||||||
|
c.Requests++
|
||||||
|
c.lastState = statue
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Breaker of circuit
|
//Breaker of circuit
|
||||||
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
||||||
|
|
||||||
cnt := NewCounter()
|
cnt := counters{}
|
||||||
|
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if cnt.ConsecutiveFailures() >= failureThreshold {
|
|
||||||
|
|
||||||
canRetry := func(cnt ICounter) bool {
|
if cnt.ConsecutiveFailures >= failureThreshold {
|
||||||
|
|
||||||
backoffLevel := cnt.ConsecutiveFailures() - failureThreshold
|
canRetry := func(cnt counters) bool {
|
||||||
|
|
||||||
|
backoffLevel := cnt.ConsecutiveFailures - failureThreshold
|
||||||
|
|
||||||
// Calculates when should the circuit breaker resume propagating requests
|
// Calculates when should the circuit breaker resume propagating requests
|
||||||
// to the service
|
// to the service
|
||||||
|
Loading…
x
Reference in New Issue
Block a user