/
mesures.go
61 lines (51 loc) · 1.03 KB
/
mesures.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
60
61
package measures
import (
"sync"
"time"
)
type FloatMeasure struct {
Key string
incrementer Incrementer
mu sync.Mutex
common float64
delta float64
syncDelta float64
}
func NewFloatMesure(key string, inc Incrementer, syncperiod time.Duration) (fm *FloatMeasure, err error) {
fm = &FloatMeasure{Key: key, incrementer: inc}
fm.common, err = inc.Get(key)
if err != nil {
return
}
go func() {
for {
fm.mu.Lock()
fm.syncDelta = fm.delta
fm.delta = 0.0
fm.mu.Unlock()
new_common, err := fm.incrementer.IncAndGet(fm.Key, fm.syncDelta)
fm.mu.Lock()
if err == nil {
fm.common = new_common
fm.syncDelta = 0.0 // this one just in case
} else {
fm.delta += fm.syncDelta
fm.syncDelta = 0.0 //this one just in case
}
fm.mu.Unlock()
time.Sleep(syncperiod)
}
}()
return
}
func (m *FloatMeasure) Inc(v float64) {
m.mu.Lock()
m.delta += v
m.mu.Unlock()
}
func (m *FloatMeasure) Get() (ret float64) {
m.mu.Lock()
ret = m.common + m.delta + m.syncDelta
m.mu.Unlock()
return
}