Add GoDoc comments to Stats type

pull/1936/head
Philip O'Toole 2015-03-13 16:17:22 -07:00 committed by Philip O'Toole
parent a99cd373bb
commit 5aa49ef823
1 changed files with 7 additions and 0 deletions

View File

@ -4,11 +4,13 @@ import (
"sync" "sync"
) )
// Int representes a 64-bit signed integer which can be updated atomically.
type Int struct { type Int struct {
mu sync.RWMutex mu sync.RWMutex
i int64 i int64
} }
// NewInt returns a new Int
func NewInt(v int64) *Int { func NewInt(v int64) *Int {
return &Int{i: v} return &Int{i: v}
} }
@ -21,12 +23,14 @@ func (i *Int) Add(delta int64) {
} }
// Stats represents a collection of metrics, as key-value pairs.
type Stats struct { type Stats struct {
name string name string
m map[string]*Int m map[string]*Int
mu sync.RWMutex mu sync.RWMutex
} }
// NewStats returns a Stats object with the given name.
func NewStats(name string) *Stats { func NewStats(name string) *Stats {
return &Stats{ return &Stats{
name: name, name: name,
@ -58,18 +62,21 @@ func (s *Stats) Inc(key string) {
s.Add(key, 1) s.Add(key, 1)
} }
// Get returns a value for a given key.
func (s *Stats) Get(key string) int64 { func (s *Stats) Get(key string) int64 {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
return s.m[key].i return s.m[key].i
} }
// Set sets a value for the given key.
func (s *Stats) Set(key string, v int64) { func (s *Stats) Set(key string, v int64) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
s.m[key] = NewInt(v) s.m[key] = NewInt(v)
} }
// Name returns the name of the Stats object.
func (s *Stats) Name() string { func (s *Stats) Name() string {
return s.name return s.name
} }