forked from kevburnsjr/microcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_func.go
99 lines (84 loc) · 1.6 KB
/
monitor_func.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
package microcache
import (
"sync"
"time"
)
// MonitorFunc turns a function into a Monitor
func MonitorFunc(interval time.Duration, logFunc func(Stats)) Monitor {
return &monitorFunc{
interval: interval,
logFunc: logFunc,
}
}
type monitorFunc struct {
interval time.Duration
logFunc func(Stats)
hits int
hitMutex sync.Mutex
misses int
missMutex sync.Mutex
stales int
staleMutex sync.Mutex
backend int
backendMutex sync.Mutex
errors int
errorMutex sync.Mutex
stop chan bool
}
func (m *monitorFunc) GetInterval() time.Duration {
return m.interval
}
func (m *monitorFunc) Log(stats Stats) {
// hits
m.hitMutex.Lock()
stats.Hits = m.hits
m.hits = 0
m.hitMutex.Unlock()
// misses
m.missMutex.Lock()
stats.Misses = m.misses
m.misses = 0
m.missMutex.Unlock()
// stales
m.staleMutex.Lock()
stats.Stales = m.stales
m.stales = 0
m.staleMutex.Unlock()
// backend
m.backendMutex.Lock()
stats.Backend = m.backend
m.backend = 0
m.backendMutex.Unlock()
// errors
m.errorMutex.Lock()
stats.Errors = m.errors
m.errors = 0
m.errorMutex.Unlock()
// log
m.logFunc(stats)
}
func (m *monitorFunc) Hit() {
m.hitMutex.Lock()
m.hits += 1
m.hitMutex.Unlock()
}
func (m *monitorFunc) Miss() {
m.missMutex.Lock()
m.misses += 1
m.missMutex.Unlock()
}
func (m *monitorFunc) Stale() {
m.staleMutex.Lock()
m.stales += 1
m.staleMutex.Unlock()
}
func (m *monitorFunc) Backend() {
m.backendMutex.Lock()
m.backend += 1
m.backendMutex.Unlock()
}
func (m *monitorFunc) Error() {
m.errorMutex.Lock()
m.errors += 1
m.errorMutex.Unlock()
}