github.com/songzhibin97/gkit@v1.2.13/window/bucket.go (about) 1 package window 2 3 import ( 4 "sync/atomic" 5 ) 6 7 const ( 8 // PtrOffSize 指针偏移量大小 9 PtrOffSize = uint64(8) 10 ) 11 12 // BucketBuilder Bucket 生成器 13 type BucketBuilder interface { 14 // NewEmptyBucket 生成一个空桶 15 NewEmptyBucket() interface{} 16 17 // Reset 重置桶 18 Reset(b *Bucket, startTime uint64) *Bucket 19 } 20 21 // Bucket 滑动窗口的承载的最小元素 22 type Bucket struct { 23 // Start 存储了这个桶的起始时间 24 Start uint64 25 26 // Value 实际挂载对象 27 Value atomic.Value 28 } 29 30 // reset 重置 Bucket.Start 属性 31 func (b *Bucket) reset(start uint64) { 32 b.Start = start 33 } 34 35 // isHit 判断 now 是否命中了该桶 36 func (b *Bucket) isHit(now uint64, bucketSize uint64) bool { 37 return b.Start <= now && now < b.Start+bucketSize 38 } 39 40 // calculateStartTime 计算初始桶的时间 41 func calculateStartTime(now uint64, bucketSize uint64) uint64 { 42 return now - (now % bucketSize) 43 }