mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 13:16:02 +03:00
update breaker patterns
This commit is contained in:
parent
04f7a40113
commit
f60c5af803
@ -21,11 +21,10 @@ const (
|
|||||||
type Breaker struct {
|
type Breaker struct {
|
||||||
errorThreshold, successThreshold int
|
errorThreshold, successThreshold int
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
lock sync.Mutex
|
||||||
lock sync.Mutex
|
state uint32
|
||||||
state uint32
|
errors, successes int
|
||||||
errors, successes int
|
lastError time.Time
|
||||||
lastError time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New constructs a new circuit-breaker that starts closed.
|
// New constructs a new circuit-breaker that starts closed.
|
||||||
|
@ -20,6 +20,27 @@ func returnsSuccess() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExampleBreaker(t *testing.T) {
|
||||||
|
breaker := New(3, 1, 5*time.Second)
|
||||||
|
|
||||||
|
for {
|
||||||
|
result := breaker.Run(func() error {
|
||||||
|
// communicate with some external service and
|
||||||
|
// return an error if the communication failed
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
switch result {
|
||||||
|
case nil:
|
||||||
|
// success!
|
||||||
|
case ErrBreakerOpen:
|
||||||
|
// our function wasn't run because the breaker was open
|
||||||
|
default:
|
||||||
|
// some other error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBreakerErrorExpiry(t *testing.T) {
|
func TestBreakerErrorExpiry(t *testing.T) {
|
||||||
breaker := New(2, 1, 1*time.Second)
|
breaker := New(2, 1, 1*time.Second)
|
||||||
|
|
||||||
@ -173,24 +194,3 @@ func TestBreakerAsyncStateTransitions(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleBreaker() {
|
|
||||||
breaker := New(3, 1, 5*time.Second)
|
|
||||||
|
|
||||||
for {
|
|
||||||
result := breaker.Run(func() error {
|
|
||||||
// communicate with some external service and
|
|
||||||
// return an error if the communication failed
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
switch result {
|
|
||||||
case nil:
|
|
||||||
// success!
|
|
||||||
case ErrBreakerOpen:
|
|
||||||
// our function wasn't run because the breaker was open
|
|
||||||
default:
|
|
||||||
// some other error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -8,13 +8,17 @@ import (
|
|||||||
|
|
||||||
//ErrServiceUnavailable for error
|
//ErrServiceUnavailable for error
|
||||||
var (
|
var (
|
||||||
ErrServiceUnavailable = errors.New("Service Unavailable")
|
ErrTooManyRequests = errors.New("too many requests")
|
||||||
|
ErrServiceUnavailable = errors.New("service unavailable")
|
||||||
FailureThreshold = 10
|
FailureThreshold = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
//State of current switch
|
//State of current switch
|
||||||
|
type State int
|
||||||
|
|
||||||
|
//states of CircuitBreaker
|
||||||
const (
|
const (
|
||||||
UnknownState uint = iota
|
UnknownState State = iota
|
||||||
FailureState
|
FailureState
|
||||||
SuccessState
|
SuccessState
|
||||||
)
|
)
|
||||||
@ -24,19 +28,19 @@ type Circuit func(context.Context) error
|
|||||||
|
|
||||||
//Counter interface
|
//Counter interface
|
||||||
type Counter interface {
|
type Counter interface {
|
||||||
Count(uint)
|
Count(State)
|
||||||
ConsecutiveFailures() uint32
|
ConsecutiveFailures() uint32
|
||||||
LastActivity() time.Time
|
LastActivity() time.Time
|
||||||
Reset()
|
Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
type counters struct {
|
type counters struct {
|
||||||
state uint
|
state State
|
||||||
lastActivity time.Time
|
lastActivity time.Time
|
||||||
counts uint32 //counts of failures
|
counts uint32 //counts of failures
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *counters) Count(state uint) {
|
func (c *counters) Count(state State) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user