googlebreaker.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package breaker
  2. import (
  3. "math"
  4. "sync/atomic"
  5. "time"
  6. "github.com/tal-tech/go-zero/core/collection"
  7. "github.com/tal-tech/go-zero/core/mathx"
  8. )
  9. const (
  10. // 250ms for bucket duration
  11. window = time.Second * 10
  12. buckets = 40
  13. k = 1.5
  14. protection = 5
  15. )
  16. // googleBreaker is a netflixBreaker pattern from google.
  17. // see Client-Side Throttling section in https://landing.google.com/sre/sre-book/chapters/handling-overload/
  18. type googleBreaker struct {
  19. k float64
  20. state int32
  21. stat *collection.RollingWindow
  22. proba *mathx.Proba
  23. }
  24. func newGoogleBreaker() *googleBreaker {
  25. bucketDuration := time.Duration(int64(window) / int64(buckets))
  26. st := collection.NewRollingWindow(buckets, bucketDuration)
  27. return &googleBreaker{
  28. stat: st,
  29. k: k,
  30. state: StateClosed,
  31. proba: mathx.NewProba(),
  32. }
  33. }
  34. func (b *googleBreaker) accept() error {
  35. accepts, total := b.history()
  36. weightedAccepts := b.k * float64(accepts)
  37. // https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101
  38. dropRatio := math.Max(0, (float64(total-protection)-weightedAccepts)/float64(total+1))
  39. if dropRatio <= 0 {
  40. if atomic.LoadInt32(&b.state) == StateOpen {
  41. atomic.CompareAndSwapInt32(&b.state, StateOpen, StateClosed)
  42. }
  43. return nil
  44. }
  45. if atomic.LoadInt32(&b.state) == StateClosed {
  46. atomic.CompareAndSwapInt32(&b.state, StateClosed, StateOpen)
  47. }
  48. if b.proba.TrueOnProba(dropRatio) {
  49. return ErrServiceUnavailable
  50. }
  51. return nil
  52. }
  53. func (b *googleBreaker) allow() (internalPromise, error) {
  54. if err := b.accept(); err != nil {
  55. return nil, err
  56. }
  57. return googlePromise{
  58. b: b,
  59. }, nil
  60. }
  61. func (b *googleBreaker) doReq(req func() error, fallback func(err error) error, acceptable Acceptable) error {
  62. if err := b.accept(); err != nil {
  63. if fallback != nil {
  64. return fallback(err)
  65. } else {
  66. return err
  67. }
  68. }
  69. defer func() {
  70. if e := recover(); e != nil {
  71. b.markFailure()
  72. panic(e)
  73. }
  74. }()
  75. err := req()
  76. if acceptable(err) {
  77. b.markSuccess()
  78. } else {
  79. b.markFailure()
  80. }
  81. return err
  82. }
  83. func (b *googleBreaker) markSuccess() {
  84. b.stat.Add(1)
  85. }
  86. func (b *googleBreaker) markFailure() {
  87. b.stat.Add(0)
  88. }
  89. func (b *googleBreaker) history() (accepts int64, total int64) {
  90. b.stat.Reduce(func(b *collection.Bucket) {
  91. accepts += int64(b.Sum)
  92. total += b.Count
  93. })
  94. return
  95. }
  96. type googlePromise struct {
  97. b *googleBreaker
  98. }
  99. func (p googlePromise) Accept() {
  100. p.b.markSuccess()
  101. }
  102. func (p googlePromise) Reject() {
  103. p.b.markFailure()
  104. }