@@ -101,11 +101,7 @@ func NewRegisteredMeterForced(name string, r Registry) Meter {
101
101
102
102
// MeterSnapshot is a read-only copy of another Meter.
103
103
type MeterSnapshot struct {
104
- // WARNING: The `temp` field is accessed atomically.
105
- // On 32 bit platforms, only 64-bit aligned fields can be atomic. The struct is
106
- // guaranteed to be so aligned, so take advantage of that. For more information,
107
- // see https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
108
- temp int64
104
+ temp atomic.Int64
109
105
count int64
110
106
rate1 , rate5 , rate15 , rateMean float64
111
107
}
@@ -173,7 +169,7 @@ type StandardMeter struct {
173
169
snapshot * MeterSnapshot
174
170
a1 , a5 , a15 EWMA
175
171
startTime time.Time
176
- stopped uint32
172
+ stopped atomic. Bool
177
173
}
178
174
179
175
func newStandardMeter () * StandardMeter {
@@ -188,8 +184,8 @@ func newStandardMeter() *StandardMeter {
188
184
189
185
// Stop stops the meter, Mark() will be a no-op if you use it after being stopped.
190
186
func (m * StandardMeter ) Stop () {
191
- stopped := atomic . SwapUint32 ( & m .stopped , 1 )
192
- if stopped != 1 {
187
+ stopped := m .stopped . Swap ( true )
188
+ if ! stopped {
193
189
arbiter .Lock ()
194
190
delete (arbiter .meters , m )
195
191
arbiter .Unlock ()
@@ -207,7 +203,7 @@ func (m *StandardMeter) Count() int64 {
207
203
208
204
// Mark records the occurrence of n events.
209
205
func (m * StandardMeter ) Mark (n int64 ) {
210
- atomic . AddInt64 ( & m .snapshot .temp , n )
206
+ m .snapshot .temp . Add ( n )
211
207
}
212
208
213
209
// Rate1 returns the one-minute moving average rate of events per second.
@@ -241,7 +237,14 @@ func (m *StandardMeter) RateMean() float64 {
241
237
// Snapshot returns a read-only copy of the meter.
242
238
func (m * StandardMeter ) Snapshot () Meter {
243
239
m .lock .RLock ()
244
- snapshot := * m .snapshot
240
+ snapshot := MeterSnapshot {
241
+ count : m .snapshot .count ,
242
+ rate1 : m .snapshot .rate1 ,
243
+ rate5 : m .snapshot .rate5 ,
244
+ rate15 : m .snapshot .rate15 ,
245
+ rateMean : m .snapshot .rateMean ,
246
+ }
247
+ snapshot .temp .Store (m .snapshot .temp .Load ())
245
248
m .lock .RUnlock ()
246
249
return & snapshot
247
250
}
@@ -257,7 +260,7 @@ func (m *StandardMeter) updateSnapshot() {
257
260
258
261
func (m * StandardMeter ) updateMeter () {
259
262
// should only run with write lock held on m.lock
260
- n := atomic . SwapInt64 ( & m .snapshot .temp , 0 )
263
+ n := m .snapshot .temp . Swap ( 0 )
261
264
m .snapshot .count += n
262
265
m .a1 .Update (n )
263
266
m .a5 .Update (n )
0 commit comments