Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: ipconfig 滑动窗口解决流量毛刺 #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ipconf/domain/window.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package domain

const (
windowSize = 5
windowSize = 5 // 防止流量毛刺,取5个点的平均值
)

type stateWindow struct {
Expand All @@ -20,14 +20,16 @@ func newStateWindow() *stateWindow {
}

func (sw *stateWindow) getStat() *Stat {
res := sw.sumStat.Clone()
res := sw.sumStat.Clone() // 因为sumStat是指针,所以需要clone一份计算均值后返回
res.Avg(windowSize)
return res
}

func (sw *stateWindow) appendStat(s *Stat) {
// 减去即将被删除的state
sw.sumStat.Sub(sw.stateQueue[sw.idx%windowSize])
// 窗口大小没达到windowSize直接入窗,否则减去窗口最前面的state
if sw.idx >= windowSize {
sw.sumStat.Sub(sw.stateQueue[sw.idx%windowSize])
}
// 更新最新的stat
sw.stateQueue[sw.idx%windowSize] = s
// 计算最新的窗口和
Expand Down