Skip to content

Commit

Permalink
Use SwapInt64 to snapshot counters (segmentio#441)
Browse files Browse the repository at this point in the history
Using LoadInt64 and then AddInt64 can lead to incoherent results if
snapshot() is called twice at the same time.
  • Loading branch information
Nicofuma authored May 6, 2020
1 parent e0af1cf commit 2213fc9
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func (c *counter) observe(v int64) {
}

func (c *counter) snapshot() int64 {
p := c.ptr()
v := atomic.LoadInt64(p)
atomic.AddInt64(p, -v)
return v
return atomic.SwapInt64(c.ptr(), 0)
}

// gauge is an atomic integer that may be set to any arbitrary value, the value
Expand Down

0 comments on commit 2213fc9

Please sign in to comment.