-
Notifications
You must be signed in to change notification settings - Fork 176
/
stats.go
77 lines (67 loc) · 1.34 KB
/
stats.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
package irc
import (
"sync"
)
type StatsValues struct {
Unknown int // unregistered clients
Total int // registered clients, including invisible
Max int // high-water mark of registered clients
Invisible int
Operators int
}
// Stats tracks statistics for a running server
type Stats struct {
StatsValues
mutex sync.Mutex
}
// Adds an unregistered client
func (s *Stats) Add() {
s.mutex.Lock()
s.Unknown += 1
s.mutex.Unlock()
}
// Transition a client from unregistered to registered
func (s *Stats) Register() {
s.mutex.Lock()
s.Unknown -= 1
s.Total += 1
if s.Max < s.Total {
s.Max = s.Total
}
s.mutex.Unlock()
}
// Modify the Invisible count
func (s *Stats) ChangeInvisible(increment int) {
s.mutex.Lock()
s.Invisible += increment
s.mutex.Unlock()
}
// Modify the Operator count
func (s *Stats) ChangeOperators(increment int) {
s.mutex.Lock()
s.Operators += increment
s.mutex.Unlock()
}
// Remove a user from the server
func (s *Stats) Remove(registered, invisible, operator bool) {
s.mutex.Lock()
if registered {
s.Total -= 1
} else {
s.Unknown -= 1
}
if invisible {
s.Invisible -= 1
}
if operator {
s.Operators -= 1
}
s.mutex.Unlock()
}
// GetStats retrives total, invisible and oper count
func (s *Stats) GetValues() (result StatsValues) {
s.mutex.Lock()
result = s.StatsValues
s.mutex.Unlock()
return
}