@@ -34,6 +34,9 @@ import (
34
34
"github.com/codahale/hdrhistogram"
35
35
)
36
36
37
+ // Note - If multiple locks must be concurrently held they should be
38
+ // acquired in this order hm, gm, cm or deadlock will result.
39
+
37
40
// A Counter is a monotonically increasing unsigned integer.
38
41
//
39
42
// Use a counter to derive rates (e.g., record total number of requests, derive
@@ -65,12 +68,12 @@ func (c Counter) SetFunc(f func() uint64) {
65
68
// the given function, with an additional initializer function for a related
66
69
// batch of counters, all of which are keyed by an arbitrary value.
67
70
func (c Counter ) SetBatchFunc (key interface {}, init func (), f func () uint64 ) {
68
- cm .Lock ()
69
- defer cm .Unlock ()
70
-
71
71
gm .Lock ()
72
72
defer gm .Unlock ()
73
73
74
+ cm .Lock ()
75
+ defer cm .Unlock ()
76
+
74
77
counterFuncs [string (c )] = f
75
78
if _ , ok := inits [key ]; ! ok {
76
79
inits [key ] = init
@@ -79,12 +82,12 @@ func (c Counter) SetBatchFunc(key interface{}, init func(), f func() uint64) {
79
82
80
83
// Remove removes the given counter.
81
84
func (c Counter ) Remove () {
82
- cm .Lock ()
83
- defer cm .Unlock ()
84
-
85
85
gm .Lock ()
86
86
defer gm .Unlock ()
87
87
88
+ cm .Lock ()
89
+ defer cm .Unlock ()
90
+
88
91
delete (counters , string (c ))
89
92
delete (counterFuncs , string (c ))
90
93
delete (inits , string (c ))
@@ -139,14 +142,14 @@ func (g Gauge) Remove() {
139
142
140
143
// Reset removes all existing counters and gauges.
141
144
func Reset () {
142
- cm .Lock ()
143
- defer cm .Unlock ()
145
+ hm .Lock ()
146
+ defer hm .Unlock ()
144
147
145
148
gm .Lock ()
146
149
defer gm .Unlock ()
147
150
148
- hm .Lock ()
149
- defer hm .Unlock ()
151
+ cm .Lock ()
152
+ defer cm .Unlock ()
150
153
151
154
counters = make (map [string ]uint64 )
152
155
counterFuncs = make (map [string ]func () uint64 )
@@ -157,14 +160,14 @@ func Reset() {
157
160
158
161
// Snapshot returns a copy of the values of all registered counters and gauges.
159
162
func Snapshot () (c map [string ]uint64 , g map [string ]int64 ) {
160
- cm .Lock ()
161
- defer cm .Unlock ()
163
+ hm .Lock ()
164
+ defer hm .Unlock ()
162
165
163
166
gm .Lock ()
164
167
defer gm .Unlock ()
165
168
166
- hm .Lock ()
167
- defer hm .Unlock ()
169
+ cm .Lock ()
170
+ defer cm .Unlock ()
168
171
169
172
for _ , init := range inits {
170
173
init ()
0 commit comments