From eb72bdc3d281d82e73767c0cb68d5db9124bdc8c Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Thu, 16 Oct 2014 15:15:19 -0700 Subject: [PATCH] stats: fix data race when recording send result --- etcdserver/stats/leader.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/etcdserver/stats/leader.go b/etcdserver/stats/leader.go index 79b28cc65..45eba2af2 100644 --- a/etcdserver/stats/leader.go +++ b/etcdserver/stats/leader.go @@ -2,6 +2,7 @@ package stats import ( "math" + "sync" "time" ) @@ -36,10 +37,15 @@ type FollowerStats struct { Fail uint64 `json:"fail"` Success uint64 `json:"success"` } `json:"counts"` + + sync.Mutex } // Succ updates the FollowerStats with a successful send func (fs *FollowerStats) Succ(d time.Duration) { + fs.Lock() + defer fs.Unlock() + total := float64(fs.Counts.Success) * fs.Latency.Average totalSquare := float64(fs.Counts.Success) * fs.Latency.averageSquare @@ -64,5 +70,7 @@ func (fs *FollowerStats) Succ(d time.Duration) { // Fail updates the FollowerStats with an unsuccessful send func (fs *FollowerStats) Fail() { + fs.Lock() + defer fs.Unlock() fs.Counts.Fail++ }