forked from VolantMQ/volantmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
70 lines (56 loc) · 1.51 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
package systree
import (
"sync/atomic"
"github.com/VolantMQ/volantmq/types"
)
type stat struct {
curr *dynamicValueInteger
max *dynamicValueInteger
}
type topicStat struct {
stat
}
type subscriptionsStat struct {
stat
}
func newStat(topicPrefix string, retained *[]types.RetainObject) stat {
s := stat{
curr: newDynamicValueInteger(topicPrefix + "/current"),
max: newDynamicValueInteger(topicPrefix + "/max"),
}
*retained = append(*retained, s.max)
*retained = append(*retained, s.curr)
return s
}
func newStatTopic(topicPrefix string, retained *[]types.RetainObject) topicStat {
return topicStat{
stat: newStat(topicPrefix+"/topics", retained),
}
}
func newStatSubscription(topicPrefix string, retained *[]types.RetainObject) subscriptionsStat {
return subscriptionsStat{
stat: newStat(topicPrefix+"/subscriptions", retained),
}
}
// Subscribed add to statistic subscriber
func (t *subscriptionsStat) Subscribed() {
newVal := atomic.AddUint64(&t.curr.val, 1)
if atomic.LoadUint64(&t.max.val) < newVal {
atomic.StoreUint64(&t.max.val, newVal)
}
}
// UnSubscribed remove subscriber from statistic
func (t *subscriptionsStat) UnSubscribed() {
atomic.AddUint64(&t.curr.val, ^uint64(0))
}
// Added add topic to statistic
func (t *topicStat) Added() {
newVal := atomic.AddUint64(&t.curr.val, 1)
if atomic.LoadUint64(&t.max.val) < newVal {
atomic.StoreUint64(&t.max.val, newVal)
}
}
// Removed remove topic from statistic
func (t *topicStat) Removed() {
atomic.AddUint64(&t.curr.val, ^uint64(0))
}