@@ -101,11 +101,7 @@ func NewRegisteredMeterForced(name string, r Registry) Meter {
101101
102102// MeterSnapshot is a read-only copy of another Meter.
103103type 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
109105 count int64
110106 rate1 , rate5 , rate15 , rateMean float64
111107}
@@ -173,7 +169,7 @@ type StandardMeter struct {
173169 snapshot * MeterSnapshot
174170 a1 , a5 , a15 EWMA
175171 startTime time.Time
176- stopped uint32
172+ stopped atomic. Bool
177173}
178174
179175func newStandardMeter () * StandardMeter {
@@ -188,8 +184,8 @@ func newStandardMeter() *StandardMeter {
188184
189185// Stop stops the meter, Mark() will be a no-op if you use it after being stopped.
190186func (m * StandardMeter ) Stop () {
191- stopped := atomic . SwapUint32 ( & m .stopped , 1 )
192- if stopped != 1 {
187+ stopped := m .stopped . Swap ( true )
188+ if ! stopped {
193189 arbiter .Lock ()
194190 delete (arbiter .meters , m )
195191 arbiter .Unlock ()
@@ -207,7 +203,7 @@ func (m *StandardMeter) Count() int64 {
207203
208204// Mark records the occurrence of n events.
209205func (m * StandardMeter ) Mark (n int64 ) {
210- atomic . AddInt64 ( & m .snapshot .temp , n )
206+ m .snapshot .temp . Add ( n )
211207}
212208
213209// Rate1 returns the one-minute moving average rate of events per second.
@@ -241,7 +237,14 @@ func (m *StandardMeter) RateMean() float64 {
241237// Snapshot returns a read-only copy of the meter.
242238func (m * StandardMeter ) Snapshot () Meter {
243239 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 ())
245248 m .lock .RUnlock ()
246249 return & snapshot
247250}
@@ -257,7 +260,7 @@ func (m *StandardMeter) updateSnapshot() {
257260
258261func (m * StandardMeter ) updateMeter () {
259262 // 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 )
261264 m .snapshot .count += n
262265 m .a1 .Update (n )
263266 m .a5 .Update (n )
0 commit comments