/
cache.go
60 lines (48 loc) · 1.2 KB
/
cache.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
package main
import (
"fmt"
"strings"
"sync"
)
type countCache struct {
sync.Mutex
last map[string]prometheusCount
next map[string]prometheusCount
}
//GetAndSwap will return the previous count for this metric and add the passed in one for future use
//bool indicates if Get was successful
func (c *countCache) GetAndSwap(n prometheusCount) (prev prometheusCount, ok bool, first bool) {
c.Lock()
defer c.Unlock()
if c == nil {
c = &countCache{}
}
if c.next == nil {
c.next = make(map[string]prometheusCount)
}
key := cacheKey(n)
c.next[key] = n
first = c.last == nil
if first {
return
}
prev, ok = c.last[key]
return
}
//indicates that a single observations sweep is done.
//this is important for determining if 'we' are new or if 'they' are
func (c *countCache) Done() {
c.Lock()
defer c.Unlock()
if c == nil {
return
}
//double map approach allows us to distinquish between no observations
//and new metric and keep memory from growing in the face of metrics
//coming and going so seems worth the complication
c.last = c.next
c.next = make(map[string]prometheusCount)
}
func cacheKey(n prometheusCount) string {
return fmt.Sprintf("%s-%s", n.Name, strings.Join(n.Tags, "-"))
}