Skip to content

Commit

Permalink
Update gauge_float64.go to reduce lock contention
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Caraveo committed Apr 5, 2018
1 parent 8732c61 commit c8b5fd4
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions gauge_float64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package metrics

import "sync"
import (
"math"
"sync/atomic"
)

// GaugeFloat64s hold a float64 value that can be set arbitrarily.
type GaugeFloat64 interface {
Expand Down Expand Up @@ -85,8 +88,7 @@ func (NilGaugeFloat64) Value() float64 { return 0.0 }
// StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
// sync.Mutex to manage a single float64 value.
type StandardGaugeFloat64 struct {
mutex sync.Mutex
value float64
value uint64
}

// Snapshot returns a read-only copy of the gauge.
Expand All @@ -96,16 +98,12 @@ func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {

// Update updates the gauge's value.
func (g *StandardGaugeFloat64) Update(v float64) {
g.mutex.Lock()
defer g.mutex.Unlock()
g.value = v
atomic.StoreUint64(&g.value, math.Float64bits(v))
}

// Value returns the gauge's current value.
func (g *StandardGaugeFloat64) Value() float64 {
g.mutex.Lock()
defer g.mutex.Unlock()
return g.value
return math.Float64frombits(atomic.LoadUint64(&g.value))
}

// FunctionalGaugeFloat64 returns value from given function
Expand Down

0 comments on commit c8b5fd4

Please sign in to comment.