blob: b1014892af273285f05afd3650282468121d6c5c [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Thellendac59e972014-08-19 18:26:11 -07005package stats
6
7import (
8 "sync"
9 "time"
10)
11
12// NewInteger creates a new Integer StatsObject with the given name and
13// returns a pointer to it.
14func 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.
25type Integer struct {
26 mu sync.RWMutex
27 lastUpdate time.Time
28 value int64
29}
30
31// Set sets the value of the object.
32func (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.
40func (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 Thellende6dcc5d2014-09-17 15:48:00 -070048func (i *Integer) LastUpdate() time.Time {
Robin Thellendac59e972014-08-19 18:26:11 -070049 i.mu.RLock()
50 defer i.mu.RUnlock()
51 return i.lastUpdate
52}
53
54// Value returns the current value of the object.
Robin Thellende6dcc5d2014-09-17 15:48:00 -070055func (i *Integer) Value() interface{} {
Robin Thellendac59e972014-08-19 18:26:11 -070056 i.mu.RLock()
57 defer i.mu.RUnlock()
58 return i.value
59}