counter.go
1.1 KB
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
package counter
import (
"sync"
)
// Counter is a counter interface.
type Counter interface {
Add(int64)
Reset()
Value() int64
}
// Group is a counter group.
type Group struct {
mu sync.RWMutex
vecs map[string]Counter
// New optionally specifies a function to generate a counter.
// It may not be changed concurrently with calls to other functions.
New func() Counter
}
// Add add a counter by a specified key, if counter not exists then make a new one and return new value.
func (g *Group) Add(key string, value int64) {
g.mu.RLock()
vec, ok := g.vecs[key]
g.mu.RUnlock()
if !ok {
vec = g.New()
g.mu.Lock()
if g.vecs == nil {
g.vecs = make(map[string]Counter)
}
if _, ok = g.vecs[key]; !ok {
g.vecs[key] = vec
}
g.mu.Unlock()
}
vec.Add(value)
}
// Value get a counter value by key.
func (g *Group) Value(key string) int64 {
g.mu.RLock()
vec, ok := g.vecs[key]
g.mu.RUnlock()
if ok {
return vec.Value()
}
return 0
}
// Reset reset a counter by key.
func (g *Group) Reset(key string) {
g.mu.RLock()
vec, ok := g.vecs[key]
g.mu.RUnlock()
if ok {
vec.Reset()
}
}