-
Notifications
You must be signed in to change notification settings - Fork 9
/
trace.go
59 lines (49 loc) · 1.02 KB
/
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
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 (this *DataTrace) SetPK(pk string) {
this.Lock()
defer this.Unlock()
// rm old caches when trace's pk changed
if this.PK != pk {
this.L = list.New()
}
this.PK = pk
}
// proposed that there were few traced items
func (this *DataTrace) Trace(pk string, v interface{}) {
this.RLock()
if this.PK != pk {
this.RUnlock()
return
}
this.RUnlock()
// we could almost not step here, so we get few wlock
this.Lock()
defer this.Unlock()
this.L.PushFront(v)
if this.L.Len() > this.MaxSize {
this.L.Remove(this.L.Back())
}
}
func (this *DataTrace) GetAllTraced() []interface{} {
this.RLock()
defer this.RUnlock()
items := make([]interface{}, 0)
for e := this.L.Front(); e != nil; e = e.Next() {
items = append(items, e)
}
return items
}