forked from qjfoidnh/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
speeds.go
60 lines (52 loc) · 1.07 KB
/
speeds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Package speeds 速度计算工具包
package speeds
import (
"sync"
"sync/atomic"
"time"
)
type (
// Speeds 统计速度
Speeds struct {
count int64
interval time.Duration // 刷新周期
nowTime time.Time
once sync.Once
}
)
func (sps *Speeds) initOnce() {
sps.once.Do(func() {
sps.nowTime = time.Now()
if sps.interval <= 0 {
sps.interval = 1 * time.Second
}
})
}
// SetInterval 设置刷新周期
func (sps *Speeds) SetInterval(interval time.Duration) {
if interval <= 0 {
return
}
sps.interval = interval
}
// Add 原子操作, 增加数据量
func (sps *Speeds) Add(count int64) {
// 初始化
sps.initOnce()
atomic.AddInt64(&sps.count, count)
}
// GetSpeeds 结束统计速度, 并返回速度
func (sps *Speeds) GetSpeeds() (speeds int64) {
sps.initOnce()
since := time.Since(sps.nowTime)
if since <= 0 {
return 0
}
speeds = int64(float64(atomic.LoadInt64(&sps.count)) * sps.interval.Seconds() / since.Seconds())
// 更新下一轮
if since >= sps.interval {
atomic.StoreInt64(&sps.count, 0)
sps.nowTime = time.Now()
}
return
}