mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 20:06:02 +03:00
fix error and make up codes for circuit breakers patters
This commit is contained in:
parent
51a141a35e
commit
278efd1524
@ -56,7 +56,8 @@ Go常用的、面向工程化和最佳实践的模式套路,包含常见的23
|
|||||||
|
|
||||||
[菜鸟教程—设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html)
|
[菜鸟教程—设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html)
|
||||||
|
|
||||||
[GO设计模式](https://github.com/senghoo/golang-design-pattern)
|
[23-Pattern-in-Go](https://github.com/senghoo/golang-design-pattern)
|
||||||
|
|
||||||
|
|
||||||
## 更多
|
## 更多
|
||||||
|
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
package profile
|
package timeprofile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
"math/big"
|
||||||
"time"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func TestBigintProfile(t *testing.T) {
|
func TestBigintProfile(t *testing.T) {
|
||||||
|
|
||||||
|
bigint := big.NewInt(14468)
|
||||||
bigint := big.NewInt(14468)
|
|
||||||
|
|
||||||
BigIntFactorial(bigint)
|
BigIntFactorial(bigint)
|
||||||
|
|
||||||
@ -21,7 +19,6 @@ func TestBigintProfile(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Duration for time differ
|
//Duration for time differ
|
||||||
func Duration(invocation time.Time, name string) {
|
func Duration(invocation time.Time, name string) {
|
||||||
elapsed := time.Since(invocation)
|
elapsed := time.Since(invocation)
|
@ -2,9 +2,14 @@ package circuit
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrServiceUnavailable = errors.New("Service Unavailable")
|
||||||
|
)
|
||||||
|
|
||||||
type State int
|
type State int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -13,6 +18,7 @@ const (
|
|||||||
SuccessState
|
SuccessState
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Counter interface
|
||||||
type Counter interface {
|
type Counter interface {
|
||||||
Count(State)
|
Count(State)
|
||||||
ConsecutiveFailures() uint32
|
ConsecutiveFailures() uint32
|
||||||
@ -20,19 +26,47 @@ type Counter interface {
|
|||||||
Reset()
|
Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type counters struct {
|
||||||
|
state State
|
||||||
|
lastActivity time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *counters) Count(State) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *counters) ConsecutiveFailures() uint32 {
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *counters) LastActivity() time.Time {
|
||||||
|
return c.lastActivity
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *counters) Reset() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCounter() Counter {
|
||||||
|
var i Counter
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
type Circuit func(context.Context) error
|
type Circuit func(context.Context) error
|
||||||
|
|
||||||
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
||||||
|
|
||||||
cnt := NewCounter()
|
cnt := NewCounter()
|
||||||
|
|
||||||
return func(ctx context) error {
|
return func(ctx context.Context) error {
|
||||||
if cnt.ConsecutiveFailures() >= failureThreshold {
|
if cnt.ConsecutiveFailures() >= failureThreshold {
|
||||||
canRetry := func(cnt Counter) {
|
canRetry := func(cnt Counter) bool {
|
||||||
backoffLevel := Cnt.ConsecutiveFailures() - failureThreshold
|
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
|
||||||
shouldRetryAt := cnt.LastActivity().Add(time.Seconds * 2 << backoffLevel)
|
shouldRetryAt := cnt.LastActivity().Add(time.Second * 2 << backoffLevel)
|
||||||
|
|
||||||
return time.Now().After(shouldRetryAt)
|
return time.Now().After(shouldRetryAt)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user