rollingwindow.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package collection
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/timex"
  6. )
  7. type (
  8. RollingWindowOption func(rollingWindow *RollingWindow)
  9. RollingWindow struct {
  10. lock sync.RWMutex
  11. size int
  12. win *window
  13. interval time.Duration
  14. offset int
  15. ignoreCurrent bool
  16. lastTime time.Duration
  17. }
  18. )
  19. func NewRollingWindow(size int, interval time.Duration, opts ...RollingWindowOption) *RollingWindow {
  20. w := &RollingWindow{
  21. size: size,
  22. win: newWindow(size),
  23. interval: interval,
  24. lastTime: timex.Now(),
  25. }
  26. for _, opt := range opts {
  27. opt(w)
  28. }
  29. return w
  30. }
  31. func (rw *RollingWindow) Add(v float64) {
  32. rw.lock.Lock()
  33. defer rw.lock.Unlock()
  34. rw.updateOffset()
  35. rw.win.add(rw.offset, v)
  36. }
  37. func (rw *RollingWindow) Reduce(fn func(b *Bucket)) {
  38. rw.lock.RLock()
  39. defer rw.lock.RUnlock()
  40. var diff int
  41. span := rw.span()
  42. // ignore current bucket, because of partial data
  43. if span == 0 && rw.ignoreCurrent {
  44. diff = rw.size - 1
  45. } else {
  46. diff = rw.size - span
  47. }
  48. if diff > 0 {
  49. offset := (rw.offset + span + 1) % rw.size
  50. rw.win.reduce(offset, diff, fn)
  51. }
  52. }
  53. func (rw *RollingWindow) span() int {
  54. offset := int(timex.Since(rw.lastTime) / rw.interval)
  55. if 0 <= offset && offset < rw.size {
  56. return offset
  57. } else {
  58. return rw.size
  59. }
  60. }
  61. func (rw *RollingWindow) updateOffset() {
  62. span := rw.span()
  63. if span > 0 {
  64. offset := rw.offset
  65. // reset expired buckets
  66. start := offset + 1
  67. steps := start + span
  68. var remainder int
  69. if steps > rw.size {
  70. remainder = steps - rw.size
  71. steps = rw.size
  72. }
  73. for i := start; i < steps; i++ {
  74. rw.win.resetBucket(i)
  75. offset = i
  76. }
  77. for i := 0; i < remainder; i++ {
  78. rw.win.resetBucket(i)
  79. offset = i
  80. }
  81. rw.offset = offset
  82. rw.lastTime = timex.Now()
  83. }
  84. }
  85. type Bucket struct {
  86. Sum float64
  87. Count int64
  88. }
  89. func (b *Bucket) add(v float64) {
  90. b.Sum += v
  91. b.Count++
  92. }
  93. func (b *Bucket) reset() {
  94. b.Sum = 0
  95. b.Count = 0
  96. }
  97. type window struct {
  98. buckets []*Bucket
  99. size int
  100. }
  101. func newWindow(size int) *window {
  102. var buckets []*Bucket
  103. for i := 0; i < size; i++ {
  104. buckets = append(buckets, new(Bucket))
  105. }
  106. return &window{
  107. buckets: buckets,
  108. size: size,
  109. }
  110. }
  111. func (w *window) add(offset int, v float64) {
  112. w.buckets[offset%w.size].add(v)
  113. }
  114. func (w *window) reduce(start, count int, fn func(b *Bucket)) {
  115. for i := 0; i < count; i++ {
  116. fn(w.buckets[(start+i)%len(w.buckets)])
  117. }
  118. }
  119. func (w *window) resetBucket(offset int) {
  120. w.buckets[offset].reset()
  121. }
  122. func IgnoreCurrentBucket() RollingWindowOption {
  123. return func(w *RollingWindow) {
  124. w.ignoreCurrent = true
  125. }
  126. }