Jelajahi Sumber

Inline the mutexes into their containers.

This'll be easier on the garbage collector, I think, and is definitely more idiomatic.
Richard Crowley 12 tahun lalu
induk
melakukan
5330744552
4 mengubah file dengan 8 tambahan dan 21 penghapusan
  1. 2 5
      ewma.go
  2. 1 4
      histogram.go
  3. 2 5
      registry.go
  4. 3 7
      sample.go

+ 2 - 5
ewma.go

@@ -23,7 +23,7 @@ type EWMA interface {
 type StandardEWMA struct {
 	alpha     float64
 	init      bool
-	mutex     *sync.Mutex
+	mutex     sync.Mutex
 	rate      float64
 	uncounted int64
 }
@@ -33,10 +33,7 @@ var _ EWMA = &StandardEWMA{}
 
 // Create a new EWMA with the given alpha.
 func NewEWMA(alpha float64) *StandardEWMA {
-	return &StandardEWMA{
-		alpha: alpha,
-		mutex: &sync.Mutex{},
-	}
+	return &StandardEWMA{alpha: alpha}
 }
 
 // Create a new EWMA with alpha set for a one-minute moving average.

+ 1 - 4
histogram.go

@@ -28,7 +28,7 @@ type Histogram interface {
 // to synchronize its calculations.
 type StandardHistogram struct {
 	count, sum, min, max int64
-	mutex                *sync.Mutex
+	mutex                sync.Mutex
 	s                    Sample
 	variance             [2]float64
 }
@@ -41,12 +41,9 @@ var _ Histogram = &StandardHistogram{}
 // for special treatment on its first iteration.
 func NewHistogram(s Sample) *StandardHistogram {
 	return &StandardHistogram{
-		count:    0,
 		max:      math.MinInt64,
 		min:      math.MaxInt64,
-		mutex:    &sync.Mutex{},
 		s:        s,
-		sum:      0,
 		variance: [2]float64{-1.0, 0.0},
 	}
 }

+ 2 - 5
registry.go

@@ -29,7 +29,7 @@ type Registry interface {
 // of names to metrics.
 type StandardRegistry struct {
 	metrics map[string]interface{}
-	mutex   *sync.Mutex
+	mutex   sync.Mutex
 }
 
 // Force the compiler to check that StandardRegistry implements Registry.
@@ -37,10 +37,7 @@ var _ Registry = &StandardRegistry{}
 
 // Create a new registry.
 func NewRegistry() *StandardRegistry {
-	return &StandardRegistry{
-		metrics: make(map[string]interface{}),
-		mutex:   &sync.Mutex{},
-	}
+	return &StandardRegistry{metrics: make(map[string]interface{})}
 }
 
 // Call the given function for each registered metric.

+ 3 - 7
sample.go

@@ -29,7 +29,7 @@ type Sample interface {
 // <http://www.research.att.com/people/Cormode_Graham/library/publications/CormodeShkapenyukSrivastavaXu09.pdf>
 type ExpDecaySample struct {
 	alpha         float64
-	mutex         *sync.Mutex
+	mutex         sync.Mutex
 	reservoirSize int
 	t0, t1        time.Time
 	values        expDecaySampleHeap
@@ -43,7 +43,6 @@ var _ Sample = &ExpDecaySample{}
 func NewExpDecaySample(reservoirSize int, alpha float64) *ExpDecaySample {
 	s := &ExpDecaySample{
 		alpha:         alpha,
-		mutex:         &sync.Mutex{},
 		reservoirSize: reservoirSize,
 		t0:            time.Now(),
 		values:        make(expDecaySampleHeap, 0, reservoirSize),
@@ -108,17 +107,14 @@ func (s *ExpDecaySample) Values() []int64 {
 //
 // <http://www.cs.umd.edu/~samir/498/vitter.pdf>
 type UniformSample struct {
-	mutex         *sync.Mutex
+	mutex         sync.Mutex
 	reservoirSize int
 	values        []int64
 }
 
 // Create a new uniform sample with the given reservoir size.
 func NewUniformSample(reservoirSize int) *UniformSample {
-	return &UniformSample{
-		mutex:         &sync.Mutex{},
-		reservoirSize: reservoirSize,
-	}
+	return &UniformSample{reservoirSize: reservoirSize}
 }
 
 // Clear all samples.