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