Merge pull request #1316 from unihorn/168

stats: fix data race when recording send result
This commit is contained in:
Yicheng Qin 2014-10-16 16:11:22 -07:00
commit 074ddb5876

View File

@ -2,6 +2,7 @@ package stats
import ( import (
"math" "math"
"sync"
"time" "time"
) )
@ -36,10 +37,15 @@ type FollowerStats struct {
Fail uint64 `json:"fail"` Fail uint64 `json:"fail"`
Success uint64 `json:"success"` Success uint64 `json:"success"`
} `json:"counts"` } `json:"counts"`
sync.Mutex
} }
// Succ updates the FollowerStats with a successful send // Succ updates the FollowerStats with a successful send
func (fs *FollowerStats) Succ(d time.Duration) { func (fs *FollowerStats) Succ(d time.Duration) {
fs.Lock()
defer fs.Unlock()
total := float64(fs.Counts.Success) * fs.Latency.Average total := float64(fs.Counts.Success) * fs.Latency.Average
totalSquare := float64(fs.Counts.Success) * fs.Latency.averageSquare 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 // Fail updates the FollowerStats with an unsuccessful send
func (fs *FollowerStats) Fail() { func (fs *FollowerStats) Fail() {
fs.Lock()
defer fs.Unlock()
fs.Counts.Fail++ fs.Counts.Fail++
} }