-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathqueue.go
52 lines (43 loc) · 933 Bytes
/
queue.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
// Queue metrics for 5min so we send less
// data to the DB and lower load/change of racing
// conditions.
package queue
import (
"sync"
)
type Stat struct {
InOctet uint32
OutOctet uint32
InPacket uint32
OutPacket uint32
}
var remains map[string]Stat
var lock *sync.Mutex
func init() {
remains = make(map[string]Stat)
lock = new(sync.Mutex)
}
// Add to queue
func Queue(user string, in uint32, out uint32, inPack uint32, outPack uint32) {
lock.Lock()
defer lock.Unlock()
remain, ok := remains[user]
if ok {
remain.InOctet += in
remain.OutOctet += out
remain.InPacket += inPack
remain.OutPacket += outPack
} else {
remain = Stat{InOctet: in, OutOctet: out, InPacket: inPack, OutPacket: outPack}
}
remains[user] = remain
}
// Empty queue and return anything in it.
func Flush() map[string]Stat {
nw := make(map[string]Stat)
lock.Lock()
out := remains
remains = nw
lock.Unlock()
return out
}