|
|
|
@ -101,11 +101,7 @@ func NewRegisteredMeterForced(name string, r Registry) Meter { |
|
|
|
|
|
|
|
|
|
// MeterSnapshot is a read-only copy of another Meter.
|
|
|
|
|
type MeterSnapshot struct { |
|
|
|
|
// WARNING: The `temp` field is accessed atomically.
|
|
|
|
|
// On 32 bit platforms, only 64-bit aligned fields can be atomic. The struct is
|
|
|
|
|
// guaranteed to be so aligned, so take advantage of that. For more information,
|
|
|
|
|
// see https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
|
|
|
|
|
temp int64 |
|
|
|
|
temp atomic.Int64 |
|
|
|
|
count int64 |
|
|
|
|
rate1, rate5, rate15, rateMean float64 |
|
|
|
|
} |
|
|
|
@ -173,7 +169,7 @@ type StandardMeter struct { |
|
|
|
|
snapshot *MeterSnapshot |
|
|
|
|
a1, a5, a15 EWMA |
|
|
|
|
startTime time.Time |
|
|
|
|
stopped uint32 |
|
|
|
|
stopped atomic.Bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newStandardMeter() *StandardMeter { |
|
|
|
@ -188,8 +184,8 @@ func newStandardMeter() *StandardMeter { |
|
|
|
|
|
|
|
|
|
// Stop stops the meter, Mark() will be a no-op if you use it after being stopped.
|
|
|
|
|
func (m *StandardMeter) Stop() { |
|
|
|
|
stopped := atomic.SwapUint32(&m.stopped, 1) |
|
|
|
|
if stopped != 1 { |
|
|
|
|
stopped := m.stopped.Swap(true) |
|
|
|
|
if !stopped { |
|
|
|
|
arbiter.Lock() |
|
|
|
|
delete(arbiter.meters, m) |
|
|
|
|
arbiter.Unlock() |
|
|
|
@ -207,7 +203,7 @@ func (m *StandardMeter) Count() int64 { |
|
|
|
|
|
|
|
|
|
// Mark records the occurrence of n events.
|
|
|
|
|
func (m *StandardMeter) Mark(n int64) { |
|
|
|
|
atomic.AddInt64(&m.snapshot.temp, n) |
|
|
|
|
m.snapshot.temp.Add(n) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Rate1 returns the one-minute moving average rate of events per second.
|
|
|
|
@ -241,7 +237,14 @@ func (m *StandardMeter) RateMean() float64 { |
|
|
|
|
// Snapshot returns a read-only copy of the meter.
|
|
|
|
|
func (m *StandardMeter) Snapshot() Meter { |
|
|
|
|
m.lock.RLock() |
|
|
|
|
snapshot := *m.snapshot |
|
|
|
|
snapshot := MeterSnapshot{ |
|
|
|
|
count: m.snapshot.count, |
|
|
|
|
rate1: m.snapshot.rate1, |
|
|
|
|
rate5: m.snapshot.rate5, |
|
|
|
|
rate15: m.snapshot.rate15, |
|
|
|
|
rateMean: m.snapshot.rateMean, |
|
|
|
|
} |
|
|
|
|
snapshot.temp.Store(m.snapshot.temp.Load()) |
|
|
|
|
m.lock.RUnlock() |
|
|
|
|
return &snapshot |
|
|
|
|
} |
|
|
|
@ -257,7 +260,7 @@ func (m *StandardMeter) updateSnapshot() { |
|
|
|
|
|
|
|
|
|
func (m *StandardMeter) updateMeter() { |
|
|
|
|
// should only run with write lock held on m.lock
|
|
|
|
|
n := atomic.SwapInt64(&m.snapshot.temp, 0) |
|
|
|
|
n := m.snapshot.temp.Swap(0) |
|
|
|
|
m.snapshot.count += n |
|
|
|
|
m.a1.Update(n) |
|
|
|
|
m.a5.Update(n) |
|
|
|
|