forked from Error404UsernameNotFound/tstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lko.go
62 lines (51 loc) · 902 Bytes
/
lko.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
62
package tstorage
import (
"sync"
)
var lkoImpl *lko
type lko struct {
lko map[uint32]*DataPoint
disabled bool
sync.RWMutex
}
func getLko() *lko {
if lkoImpl == nil {
lkoImpl = &lko{}
lkoImpl.lko = make(map[uint32]*DataPoint)
}
return lkoImpl
}
func (s *lko) poll(metric uint32) *DataPoint {
if s.disabled {
return nil
}
s.RLock()
defer s.RUnlock()
return s.lko[metric]
}
func (s *lko) acceptRow(row Row) bool {
return s.accept(row.Metric, &row.DataPoint)
}
func (s *lko) accept(metric uint32, point *DataPoint) bool {
if s.disabled {
return false
}
cur := s.lko[metric]
if cur == nil {
s.Lock()
s.lko[metric] = point
defer s.Unlock()
return true
} else {
if point.Timestamp > cur.Timestamp {
s.Lock()
s.lko[metric] = point
defer s.Unlock()
return true
}
}
return false
}
func (s *lko) clear() {
s.lko = make(map[uint32]*DataPoint)
}