-
Notifications
You must be signed in to change notification settings - Fork 10
/
bandwidth.go
46 lines (39 loc) · 945 Bytes
/
bandwidth.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
package main
import (
"sync"
"time"
)
// Bandwidth struct
type Bandwidth struct {
sync.Mutex
Upload uint64
Download uint64
Last int64
}
// IncreaseUpload for increase uploaded size
func (bandwidth *Bandwidth) IncreaseUpload(size uint64) {
bandwidth.Lock()
defer bandwidth.Unlock()
bandwidth.Upload += size
bandwidth.Last = time.Now().Unix()
}
// IncreaseDownload for increase downloaded size
func (bandwidth *Bandwidth) IncreaseDownload(size uint64) {
bandwidth.Lock()
defer bandwidth.Unlock()
bandwidth.Download += size
bandwidth.Last = time.Now().Unix()
}
// Reset for reset uploaded and downloaded size
func (bandwidth *Bandwidth) Reset() {
bandwidth.Lock()
defer bandwidth.Unlock()
bandwidth.Upload = 0
bandwidth.Download = 0
bandwidth.Last = time.Now().Unix()
}
func newBandwidth() *Bandwidth {
bandwidth := Bandwidth{}
bandwidth.Last = 0
return &bandwidth
}