...

Source file src/github.com/go-kit/kit/metrics/internal/ratemap/ratemap.go

Documentation: github.com/go-kit/kit/metrics/internal/ratemap

     1  // Package ratemap implements a goroutine-safe map of string to float64. It can
     2  // be embedded in implementations whose metrics support fixed sample rates, so
     3  // that an additional parameter doesn't have to be tracked through the e.g.
     4  // lv.Space object.
     5  package ratemap
     6  
     7  import "sync"
     8  
     9  // RateMap is a simple goroutine-safe map of string to float64.
    10  type RateMap struct {
    11  	mtx sync.RWMutex
    12  	m   map[string]float64
    13  }
    14  
    15  // New returns a new RateMap.
    16  func New() *RateMap {
    17  	return &RateMap{
    18  		m: map[string]float64{},
    19  	}
    20  }
    21  
    22  // Set writes the given name/rate pair to the map.
    23  // Set is safe for concurrent access by multiple goroutines.
    24  func (m *RateMap) Set(name string, rate float64) {
    25  	m.mtx.Lock()
    26  	defer m.mtx.Unlock()
    27  	m.m[name] = rate
    28  }
    29  
    30  // Get retrieves the rate for the given name, or 1.0 if none is set.
    31  // Get is safe for concurrent access by multiple goroutines.
    32  func (m *RateMap) Get(name string) float64 {
    33  	m.mtx.RLock()
    34  	defer m.mtx.RUnlock()
    35  	f, ok := m.m[name]
    36  	if !ok {
    37  		f = 1.0
    38  	}
    39  	return f
    40  }
    41  

View as plain text