-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
121 lines (97 loc) · 2.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package stats
import (
"sync"
)
var (
stats = NewStats()
)
// Stats represents numeric statistics. Simplest counter that can be increased
// and decreased. Thread-safe.
type Stats struct {
*sync.RWMutex
items map[string]int
}
// NewStats creates new stats. Use this method if global stats is not enough
// for you.
func NewStats() *Stats {
return &Stats{
RWMutex: &sync.RWMutex{},
items: map[string]int{},
}
}
// Set given value for given key.
func (stats *Stats) Set(key string, value int) {
stats.Lock()
defer stats.Unlock()
stats.items[key] = value
}
// Increase counter with given key by 1.
func (stats *Stats) Increase(key string) {
stats.Lock()
defer stats.Unlock()
stats.items[key] += 1
}
// IncreaseMany counter with given key by given `many` number.
func (stats *Stats) IncreaseMany(key string, many int) {
stats.Lock()
defer stats.Unlock()
stats.items[key] += many
}
// Decrease counter with key by 1, value will not go less than zero.
func (stats *Stats) Decrease(key string) {
stats.Lock()
defer stats.Unlock()
value, _ := stats.items[key]
if value > 0 {
stats.items[key] = value - 1
} else {
stats.items[key] = 0
}
}
// DecreaseMany counter with key by given `many` number, value will not go less
// than zero.
func (stats *Stats) DecreaseMany(key string, many int) {
stats.Lock()
defer stats.Unlock()
value, _ := stats.items[key]
if value > 0 && value-many >= 0 {
stats.items[key] = value - many
} else {
stats.items[key] = 0
}
}
// Get returns table with specified keys and counters.
func (stats *Stats) Get() map[string]int {
cloned := map[string]int{}
stats.RLock()
defer stats.RUnlock()
for key, value := range stats.items {
cloned[key] = value
}
return cloned
}
// Set given value for given key.
func Set(key string, value int) {
stats.Set(key, value)
}
// Increase counter with given key by 1.
func Increase(key string) {
stats.Increase(key)
}
// IncreaseMany counter with given key by given `many` number.
func IncreaseMany(key string, many int) {
stats.IncreaseMany(key, many)
}
// Decrease counter with key by 1, value will not go less than zero.
func Decrease(key string) {
stats.Decrease(key)
}
// DecreaseMany counter with key by given `many` number, value will not go less
// than zero.
func DecreaseMany(key string, many int) {
stats.DecreaseMany(key, many)
}
// Get returns table with specified keys and counters.
func Get() map[string]int {
return stats.Get()
}