瀏覽代碼

align bucket boundary to interval in rolling window (#345)

Kevin Wan 4 年之前
父節點
當前提交
0786862a35
共有 2 個文件被更改,包括 8 次插入1 次删除
  1. 3 1
      core/collection/rollingwindow.go
  2. 5 0
      core/collection/rollingwindow_test.go

+ 3 - 1
core/collection/rollingwindow.go

@@ -96,7 +96,9 @@ func (rw *RollingWindow) updateOffset() {
 	}
 	}
 
 
 	rw.offset = (offset + span) % rw.size
 	rw.offset = (offset + span) % rw.size
-	rw.lastTime = rw.lastTime + rw.interval*time.Duration(span)
+	now := timex.Now()
+	// align to interval time boundary
+	rw.lastTime = now - (now-rw.lastTime)%rw.interval
 }
 }
 
 
 type Bucket struct {
 type Bucket struct {

+ 5 - 0
core/collection/rollingwindow_test.go

@@ -129,6 +129,11 @@ func TestRollingWindowBucketTimeBoundary(t *testing.T) {
 	r.Add(5)
 	r.Add(5)
 	r.Add(6)
 	r.Add(6)
 	assert.Equal(t, []float64{1, 5, 15}, listBuckets())
 	assert.Equal(t, []float64{1, 5, 15}, listBuckets())
+	time.Sleep(time.Millisecond * 100)
+	r.Add(7)
+	r.Add(8)
+	r.Add(9)
+	assert.Equal(t, []float64{0, 0, 24}, listBuckets())
 }
 }
 
 
 func TestRollingWindowDataRace(t *testing.T) {
 func TestRollingWindowDataRace(t *testing.T) {