vendor: fix data race in github.com/armon/go-metrics/start.go

96 -> 42 data races
This commit is contained in:
Frank Schroeder 2017-05-22 21:39:36 +02:00
parent 3caadb37ac
commit d5f87a20cf
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 9 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package metrics
import ( import (
"os" "os"
"sync/atomic"
"time" "time"
) )
@ -25,11 +26,10 @@ type Metrics struct {
} }
// Shared global metrics instance // Shared global metrics instance
var globalMetrics *Metrics var global atomic.Value
func init() { func init() {
// Initialize to a blackhole sink to avoid errors global.Store(&Metrics{sink: &BlackholeSink{}})
globalMetrics = &Metrics{sink: &BlackholeSink{}}
} }
// DefaultConfig provides a sane default configuration // DefaultConfig provides a sane default configuration
@ -68,28 +68,28 @@ func New(conf *Config, sink MetricSink) (*Metrics, error) {
func NewGlobal(conf *Config, sink MetricSink) (*Metrics, error) { func NewGlobal(conf *Config, sink MetricSink) (*Metrics, error) {
metrics, err := New(conf, sink) metrics, err := New(conf, sink)
if err == nil { if err == nil {
globalMetrics = metrics global.Store(metrics)
} }
return metrics, err return metrics, err
} }
// Proxy all the methods to the globalMetrics instance // Proxy all the methods to the globalMetrics instance
func SetGauge(key []string, val float32) { func SetGauge(key []string, val float32) {
globalMetrics.SetGauge(key, val) global.Load().(*Metrics).SetGauge(key, val)
} }
func EmitKey(key []string, val float32) { func EmitKey(key []string, val float32) {
globalMetrics.EmitKey(key, val) global.Load().(*Metrics).EmitKey(key, val)
} }
func IncrCounter(key []string, val float32) { func IncrCounter(key []string, val float32) {
globalMetrics.IncrCounter(key, val) global.Load().(*Metrics).IncrCounter(key, val)
} }
func AddSample(key []string, val float32) { func AddSample(key []string, val float32) {
globalMetrics.AddSample(key, val) global.Load().(*Metrics).AddSample(key, val)
} }
func MeasureSince(key []string, start time.Time) { func MeasureSince(key []string, start time.Time) {
globalMetrics.MeasureSince(key, start) global.Load().(*Metrics).MeasureSince(key, start)
} }