-
Notifications
You must be signed in to change notification settings - Fork 2
/
nonce.go
52 lines (43 loc) · 860 Bytes
/
nonce.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
package nonce
import (
"strconv"
"sync"
)
// Nonce struct holds the nonce value
type Nonce struct {
n int64
m sync.Mutex
}
// Inc increments the nonce value
func (n *Nonce) Inc() {
n.m.Lock()
n.n++
n.m.Unlock()
}
// Get retrives the nonce value
func (n *Nonce) Get() Value {
n.m.Lock()
defer n.m.Unlock()
return Value(n.n)
}
// GetInc increments and returns the value of the nonce
func (n *Nonce) GetInc() Value {
n.Inc()
return n.Get()
}
// Set sets the nonce value
func (n *Nonce) Set(val int64) {
n.m.Lock()
n.n = val
n.m.Unlock()
}
// String returns a string version of the nonce
func (n *Nonce) String() string {
return n.Get().String()
}
// Value is a return type for GetValue
type Value int64
// String is a Value method that changes format to a string
func (v Value) String() string {
return strconv.FormatInt(int64(v), 10)
}