-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.go
92 lines (74 loc) · 1.39 KB
/
stat.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
package connpool
import "sync/atomic"
type counter interface {
inc() (newVal int)
dec() (newVal int)
reset() (newVal int)
val() int
}
type availabler interface {
available() int
}
type count struct {
v int64
}
func newCounter() counter {
return &count{}
}
func (c *count) inc() (new int) {
return int(atomic.AddInt64(&c.v, 1))
}
func (c *count) dec() (new int) {
return int(atomic.AddInt64(&c.v, -1))
}
func (c count) val() int {
return int(atomic.LoadInt64(&c.v))
}
func (c *count) reset() (old int) {
return int(atomic.SwapInt64(&c.v, 0))
}
type stats struct {
a availabler
size counter
request counter
success counter
}
func newStats(a availabler) *stats {
return &stats{
a: a,
size: newCounter(),
request: newCounter(),
success: newCounter(),
}
}
func (s *stats) reset() {
s.size.reset()
s.success.reset()
s.request.reset()
}
func (s *stats) snapshot() Stats {
return &statsSnapshot{
available: s.a.available(),
size: s.size.val(),
request: s.request.val(),
success: s.success.val(),
}
}
type statsSnapshot struct {
available int
size int
request int
success int
}
func (s *statsSnapshot) Available() int {
return s.available
}
func (s *statsSnapshot) Request() int {
return s.request
}
func (s *statsSnapshot) Success() int {
return s.success
}
func (s *statsSnapshot) Active() int {
return s.size - s.available
}