golang 实现时间滑动窗口的示例代码
作者:wangxiaoangg 时间:2024-02-13 18:22:18
一 概念
固定窗口就像是滑动窗口的一个特例,固定窗口是大小固定且不能随着时间而变化的。
滑动时间窗口就是把一段时间片分为多个样本窗口,可以通过更细粒度对数据进行统计。然后计算对应的时间落在那个窗口上,来对数据统计;滑动时间窗口,随着时间流失,最开始的样本窗口将会失效,同时会生成新的样本窗口。
例如 我们将1s划分为4个样本窗口,每个样本窗口对应250ms。
二 go-zero中的滑动窗口实现
1.Bucket 样本窗口
Bucket用于记录每个样本窗口的值
// Bucket defines the bucket that holds sum and num of additions.
type Bucket struct {
Sum float64 //样本窗口的值
Count int64 //样本窗口被add的次数
}
func (b *Bucket) add(v float64) {
b.Sum += v
b.Count++
}
//重置样本窗口,样本窗口过期时
func (b *Bucket) reset() {
b.Sum = 0
b.Count = 0
}
2. window 滑动窗口
type window struct {
buckets []*Bucket //样本窗口
size int //样本窗口个数
}
func newWindow(size int) *window {
buckets := make([]*Bucket, size)
for i := 0; i < size; i++ {
buckets[i] = new(Bucket)
}
return &window{
buckets: buckets,
size: size,
}
}
func (w *window) add(offset int, v float64) {
w.buckets[offset%w.size].add(v)
}
func (w *window) reduce(start, count int, fn func(b *Bucket)) {
for i := 0; i < count; i++ {
fn(w.buckets[(start+i)%w.size])
}
}
func (w *window) resetBucket(offset int) {
w.buckets[offset%w.size].reset()
}
3. RollingWindow窗口
bucket和window的实现都很简单,逻辑很好理解。
RollingWindow相对复杂一些。
当add值时需要如下操作:
计算已经过期的bucket(样本窗口),将已经过期的bucket重置。
计算offset,当前add操作应当记录到哪个bucket中。
type (
// RollingWindowOption let callers customize the RollingWindow.
RollingWindowOption func(rollingWindow *RollingWindow)
// RollingWindow defines a rolling window to calculate the events in buckets with time interval.
RollingWindow struct {
lock sync.RWMutex
size int
win *window
interval time.Duration
offset int
ignoreCurrent bool
lastTime time.Duration // start time of the last bucket
}
)
// NewRollingWindow returns a RollingWindow that with size buckets and time interval,
// use opts to customize the RollingWindow.
func NewRollingWindow(size int, interval time.Duration, opts ...RollingWindowOption) *RollingWindow {
if size < 1 {
panic("size must be greater than 0")
}
w := &RollingWindow{
size: size,
win: newWindow(size),
interval: interval,
lastTime: timex.Now(),
}
for _, opt := range opts {
opt(w)
}
return w
}
// Add adds value to current bucket.
func (rw *RollingWindow) Add(v float64) {
rw.lock.Lock()
defer rw.lock.Unlock()
rw.updateOffset()
rw.win.add(rw.offset, v)
}
// Reduce runs fn on all buckets, ignore current bucket if ignoreCurrent was set.
func (rw *RollingWindow) Reduce(fn func(b *Bucket)) {
rw.lock.RLock()
defer rw.lock.RUnlock()
var diff int
//获取跨度,并计算还有几个bucket还在窗口期内
span := rw.span()
// ignore current bucket, because of partial data
if span == 0 && rw.ignoreCurrent {
diff = rw.size - 1
} else {
diff = rw.size - span
}
if diff > 0 {
offset := (rw.offset + span + 1) % rw.size
rw.win.reduce(offset, diff, fn)
}
}
//距离上次add操作跨度,
//例如 lastTime = 1s, 当前时间1777ms。样本窗口时间250ms,那么跨度为3个样本窗口
func (rw *RollingWindow) span() int {
offset := int(timex.Since(rw.lastTime) / rw.interval)
if 0 <= offset && offset < rw.size {
return offset
}
return rw.size
}
//g
func (rw *RollingWindow) updateOffset() {
span := rw.span()
if span <= 0 {
return
}
offset := rw.offset
// reset expired buckets ,重置已经超时的bucket
for i := 0; i < span; i++ {
rw.win.resetBucket((offset + i + 1) % rw.size)
}
rw.offset = (offset + span) % rw.size
now := timex.Now()
//和样本窗口时间对齐
rw.lastTime = now - (now-rw.lastTime)%rw.interval
}
三 使用
//1.新建一个4样本窗口,每个样本窗口250ms
rollingWindow:= NewRollingWindow(4, time.Millisecond*250,IgnoreCurrentBucket())
//2.add
rollingWindow.Add(1)
rollingWindow.Add(2)
time.Sleep(time.Millisecond*250)
rollingWindow.Add(3)
rollingWindow.Add(4)
//3.获取滑动窗口的值
var Sum float64
var total int64
rollingWindow.Reduce(func(b *collection.Bucket) {
Sum += int64(b.Sum)
total += b.Count
})
来源:https://blog.csdn.net/qq_16399991/article/details/127239852
标签:golang,滑动窗口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python链表的基础概念和基础用法详解
2021-02-26 07:13:50
![](https://img.aspxhome.com/file/2023/5/80435_0s.jpg)
Makefile构建Golang项目示例详解
2024-05-25 15:16:26
JavaScript Dom编程:介绍学习书籍
2008-02-20 08:32:00
Python封装shell命令实例分析
2022-03-11 13:39:37
Python实现二叉堆
2023-12-24 13:40:48
![](https://img.aspxhome.com/file/2023/6/133796_0s.png)
将mater库中的系统存储过程批量生成*.sql文件 通用且非常实用
2012-06-06 20:03:43
Python判断文件或文件夹是否存在的三种方法
2021-04-25 21:53:06
Django使用unittest模块进行单元测试过程解析
2021-04-03 13:09:08
Python 200行代码实现一个滑动验证码过程详解
2022-08-24 18:24:35
网页设计五原则
2007-11-03 13:50:00
CSS扫盲(一): padding
2009-08-04 17:58:00
ASP用JAVASCRIPT脚本实现分页的办法
2007-10-30 13:18:00
python 实现简单的吃豆人游戏
2023-12-02 22:00:59
![](https://img.aspxhome.com/file/2023/5/97645_0s.gif)
VueJs 将接口用webpack代理到本地的方法
2024-06-05 15:30:50
![](https://img.aspxhome.com/file/2023/3/123133_0s.png)
将ASP记录集输出成n列的表格形式显示的方法
2011-04-08 10:51:00
Python删除指定目录下过期文件的2个脚本分享
2022-09-11 19:01:10
Python实现模拟登录及表单提交的方法
2021-05-28 19:23:12
MySQL学习笔记4:完整性约束限制字段
2024-01-27 15:54:29
Python中列表、字典、元组、集合数据结构整理
2022-08-02 05:54:57
pytorch训练神经网络爆内存的解决方案
2023-07-06 01:46:24
![](https://img.aspxhome.com/file/2023/2/62292_0s.png)