-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
62 lines (50 loc) · 1008 Bytes
/
trace.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 proc
import (
"container/list"
"sync"
)
type DataTrace struct {
sync.RWMutex
MaxSize int
Name string
PK string
L *list.List
}
func NewDataTrace(name string, maxSize int) *DataTrace {
return &DataTrace{L: list.New(), Name: name, MaxSize: maxSize}
}
func (u *DataTrace) SetPK(pk string) {
u.Lock()
defer u.Unlock()
// rm old caches when trace's pk changed
if u.PK != pk {
u.L = list.New()
}
u.PK = pk
}
// proposed that there were few traced items
func (u *DataTrace) Trace(pk string, v interface{}) {
u.RLock()
if u.PK != pk {
u.RUnlock()
return
}
u.RUnlock()
// we could almost not step here, so we get few wlock
u.Lock()
defer u.Unlock()
u.L.PushFront(v)
if u.L.Len() > u.MaxSize {
u.L.Remove(u.L.Back())
}
}
func (u *DataTrace) GetAllTraced() []interface{} {
var items []interface{}
u.RLock()
defer u.RUnlock()
items = make([]interface{}, 0)
for e := u.L.Front(); e != nil; e = e.Next() {
items = append(items, e)
}
return items
}