-
Notifications
You must be signed in to change notification settings - Fork 2
/
speed.go
83 lines (68 loc) · 1.39 KB
/
speed.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2022 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TRU calculate speed per second module
package tru
import (
"sync"
"time"
)
const sumArrayLen = 10
type speed struct {
speed int
current int
currentSum int
sumArray [sumArrayLen]int
timer *time.Timer
sync.RWMutex
}
// newSpeed create new packet speed calculator
func (s *speed) init() {
s.process()
}
// destroy packet speed calculator
func (s *speed) destroy() {
s.Lock()
defer s.Unlock()
if s.timer != nil {
s.timer.Stop()
}
}
// get current speed
func (s *speed) get() int {
s.RLock()
defer s.RUnlock()
return s.speed
}
// add packet to speed calculator
func (s *speed) add(count ...bool) {
s.Lock()
defer s.Unlock()
now := time.Now()
unixNano := now.UnixNano()
umillisec := unixNano / 1000000
i := int((umillisec / 100) % 10)
if i != s.current {
s.sumArray[s.current] = s.currentSum
speed := 0
for _, v := range s.sumArray {
speed += v
}
s.speed = speed
s.currentSum = 0
s.current = i
}
if len(count) == 0 {
s.currentSum++
}
}
// process executes 10 times per second to switch speed array when packets not
// received
func (s *speed) process() {
s.Lock()
defer s.Unlock()
s.timer = time.AfterFunc(100*time.Millisecond, func() {
s.add(false)
s.process()
})
}