/
counter.go
57 lines (54 loc) · 1.38 KB
/
counter.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
package main
import (
"reflect"
"strconv"
"sync"
)
type Counter struct {
Mutex sync.Mutex
Value uint32
DB Database
testing bool
namespace string
}
func (Count *Counter) Init(DB Database) {
logger.Debug("Initializing", "function", "Init", "struct", "Counter")
Count.DB = DB
Count.Mutex.Lock()
defer Count.Mutex.Unlock()
if Count.DB != nil && Count.DB.IsInitialized() {
Count.namespace = Count.DB.GetSystemNS()
val, err := Count.DB.Get(Count.namespace, "counter")
Count.Value = 0
logger.Debug("Get count from db", "function", "Init", "struct", "Counter", "value", val, "type", reflect.TypeOf(val))
if err == nil {
fromString, err := strconv.ParseInt(val, 10, 32)
if err != nil {
panic(err)
}
Count.Value = uint32(fromString)
}
} else {
Count.Value = 0
}
logger.Debug("Initialization complete", "function", "Init", "struct", "Counter")
}
func (Count *Counter) GetCount() uint32 {
Count.Mutex.Lock()
defer Count.Mutex.Unlock()
var currentCount = Count.Value
Count.Value = Count.Value + 1
if Count.DB != nil && Count.DB.IsInitialized() {
Count.DB.Set(Count.namespace, "counter", Count.Value)
} else {
if !Count.testing {
logger.Error("Not initialized", "function", "GetCount", "struct", "Counter")
}
}
return currentCount
}
func (Count *Counter) PeakCount() uint32 {
Count.Mutex.Lock()
defer Count.Mutex.Unlock()
return Count.Value
}