-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonitor_func.go
89 lines (69 loc) · 1.59 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
package microcache
import (
"sync/atomic"
"time"
)
// MonitorFunc turns a function into a Monitor
func MonitorFunc(interval time.Duration, logFunc func(Stats)) *monitorFunc {
return &monitorFunc{
interval: interval,
logFunc: logFunc,
}
}
type monitorFunc struct {
interval time.Duration
logFunc func(Stats)
hits int64
misses int64
stales int64
backend int64
errors int64
stop chan bool
}
func (m *monitorFunc) GetInterval() time.Duration {
return m.interval
}
func (m *monitorFunc) Log(stats Stats) {
// hits
stats.Hits = int(atomic.SwapInt64(&m.hits, 0))
// misses
stats.Misses = int(atomic.SwapInt64(&m.misses, 0))
// stales
stats.Stales = int(atomic.SwapInt64(&m.stales, 0))
// backend
stats.Backend = int(atomic.SwapInt64(&m.backend, 0))
// errors
stats.Errors = int(atomic.SwapInt64(&m.errors, 0))
// log
m.logFunc(stats)
}
func (m *monitorFunc) Hit() {
atomic.AddInt64(&m.hits, 1)
}
func (m *monitorFunc) Miss() {
atomic.AddInt64(&m.misses, 1)
}
func (m *monitorFunc) Stale() {
atomic.AddInt64(&m.stales, 1)
}
func (m *monitorFunc) Backend() {
atomic.AddInt64(&m.backend, 1)
}
func (m *monitorFunc) Error() {
atomic.AddInt64(&m.errors, 1)
}
func (m *monitorFunc) getHits() int {
return int(atomic.LoadInt64(&m.hits))
}
func (m *monitorFunc) getMisses() int {
return int(atomic.LoadInt64(&m.misses))
}
func (m *monitorFunc) getStales() int {
return int(atomic.LoadInt64(&m.stales))
}
func (m *monitorFunc) getBackends() int {
return int(atomic.LoadInt64(&m.backend))
}
func (m *monitorFunc) getErrors() int {
return int(atomic.LoadInt64(&m.errors))
}