Просмотр исходного кода

Fix data race in Sample.Count() method

Artyom Pervukhin 11 лет назад
Родитель
Сommit
05d164201f
1 измененных файлов с 6 добавлено и 3 удалено
  1. 6 3
      sample.go

+ 6 - 3
sample.go

@@ -6,7 +6,6 @@ import (
 	"math/rand"
 	"math/rand"
 	"sort"
 	"sort"
 	"sync"
 	"sync"
-	"sync/atomic"
 	"time"
 	"time"
 )
 )
 
 
@@ -74,7 +73,9 @@ func (s *ExpDecaySample) Clear() {
 // Count returns the number of samples recorded, which may exceed the
 // Count returns the number of samples recorded, which may exceed the
 // reservoir size.
 // reservoir size.
 func (s *ExpDecaySample) Count() int64 {
 func (s *ExpDecaySample) Count() int64 {
-	return atomic.LoadInt64(&s.count)
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	return s.count
 }
 }
 
 
 // Max returns the maximum value in the sample, which may not be the maximum
 // Max returns the maximum value in the sample, which may not be the maximum
@@ -415,7 +416,9 @@ func (s *UniformSample) Clear() {
 // Count returns the number of samples recorded, which may exceed the
 // Count returns the number of samples recorded, which may exceed the
 // reservoir size.
 // reservoir size.
 func (s *UniformSample) Count() int64 {
 func (s *UniformSample) Count() int64 {
-	return atomic.LoadInt64(&s.count)
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	return s.count
 }
 }
 
 
 // Max returns the maximum value in the sample, which may not be the maximum
 // Max returns the maximum value in the sample, which may not be the maximum