Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 5 | package stats |
| 6 | |
| 7 | import ( |
| 8 | "sync" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // NewInteger creates a new Integer StatsObject with the given name and |
| 13 | // returns a pointer to it. |
| 14 | func NewInteger(name string) *Integer { |
| 15 | lock.Lock() |
| 16 | defer lock.Unlock() |
| 17 | |
| 18 | node := findNodeLocked(name, true) |
| 19 | i := Integer{value: 0} |
| 20 | node.object = &i |
| 21 | return &i |
| 22 | } |
| 23 | |
| 24 | // Integer implements the StatsObject interface. |
| 25 | type Integer struct { |
| 26 | mu sync.RWMutex |
| 27 | lastUpdate time.Time |
| 28 | value int64 |
| 29 | } |
| 30 | |
| 31 | // Set sets the value of the object. |
| 32 | func (i *Integer) Set(value int64) { |
| 33 | i.mu.Lock() |
| 34 | defer i.mu.Unlock() |
| 35 | i.lastUpdate = time.Now() |
| 36 | i.value = value |
| 37 | } |
| 38 | |
| 39 | // Incr increments the value of the object. |
| 40 | func (i *Integer) Incr(delta int64) { |
| 41 | i.mu.Lock() |
| 42 | defer i.mu.Unlock() |
| 43 | i.value += delta |
| 44 | i.lastUpdate = time.Now() |
| 45 | } |
| 46 | |
| 47 | // LastUpdate returns the time at which the object was last updated. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 48 | func (i *Integer) LastUpdate() time.Time { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 49 | i.mu.RLock() |
| 50 | defer i.mu.RUnlock() |
| 51 | return i.lastUpdate |
| 52 | } |
| 53 | |
| 54 | // Value returns the current value of the object. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 55 | func (i *Integer) Value() interface{} { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 56 | i.mu.RLock() |
| 57 | defer i.mu.RUnlock() |
| 58 | return i.value |
| 59 | } |