Commit edeb2d90 authored by Roman Proskuryakov's avatar Roman Proskuryakov

Prevent Meter from being added twice

parent f120cc70
...@@ -32,6 +32,7 @@ func (s Snapshot) String() string { ...@@ -32,6 +32,7 @@ func (s Snapshot) String() string {
// Meter is a meter for monitoring a flow. // Meter is a meter for monitoring a flow.
type Meter struct { type Meter struct {
accumulator uint64 accumulator uint64
registered int32 // atomic bool
// Take lock. // Take lock.
snapshot Snapshot snapshot Snapshot
...@@ -39,10 +40,15 @@ type Meter struct { ...@@ -39,10 +40,15 @@ type Meter struct {
// Mark updates the total. // Mark updates the total.
func (m *Meter) Mark(count uint64) { func (m *Meter) Mark(count uint64) {
if count > 0 && atomic.AddUint64(&m.accumulator, count) == count { if count > 0 {
// I'm the first one to bump this above 0. if atomic.AddUint64(&m.accumulator, count) == count {
// Register it. just_registered := atomic.CompareAndSwapInt32(&m.registered, 0, 1)
globalSweeper.Register(m) if just_registered {
// I'm the first one to bump this above 0.
// Register it.
globalSweeper.Register(m)
}
}
} }
} }
......
...@@ -113,6 +113,7 @@ func (sw *sweeper) update() { ...@@ -113,6 +113,7 @@ func (sw *sweeper) update() {
// Mark this as idle by zeroing the accumulator. // Mark this as idle by zeroing the accumulator.
swappedTotal := atomic.SwapUint64(&m.accumulator, 0) swappedTotal := atomic.SwapUint64(&m.accumulator, 0)
atomic.StoreInt32(&m.registered, 0)
// So..., are we really idle? // So..., are we really idle?
if swappedTotal > total { if swappedTotal > total {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment