-
Notifications
You must be signed in to change notification settings - Fork 2
/
leakLogic.go
62 lines (51 loc) · 978 Bytes
/
leakLogic.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 mace
import (
"container/heap"
"fmt"
"time"
)
type disposeItem struct {
value string
disposeTime time.Time
index int
}
type leakQueue []*disposeItem
func (lq leakQueue) Len() int {
return len(lq)
}
func (lq leakQueue) Less(i, j int) bool {
if lq[i].disposeTime.Sub(lq[j].disposeTime) <= 0 {
return true
}
return false
}
func (lq leakQueue) Swap(i, j int) {
lq[i], lq[j] = lq[j], lq[i]
lq[i].index = i
lq[j].index = j
return
}
func (lq leakQueue) String() string {
var s string
for _, i := range lq {
s = s + fmt.Sprintf("Value: %s, disposeTime %s\n", i.value, i.disposeTime)
}
return s
}
func (lq *leakQueue) Push(key interface{}) {
n := len(*lq)
item := key.(*disposeItem)
item.index = n
*lq = append(*lq, item)
return
}
func (lq *leakQueue) Pop() interface{} {
old := *lq
n := len(old)
item := old[n-1]
*lq = old[0 : n-1]
return item
}
func (lq *leakQueue) update(item *disposeItem) {
heap.Fix(lq, item.index)
}