-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_delay.go
More file actions
39 lines (33 loc) · 887 Bytes
/
heap_delay.go
File metadata and controls
39 lines (33 loc) · 887 Bytes
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
package workqueue
// delayHeap: min-heap by delayUntil time.
type delayHeap[K comparable, V comparable] []*node[K, V]
func (h delayHeap[K, V]) Len() int { return len(h) }
func (h delayHeap[K, V]) Less(i, j int) bool {
// Earlier delayUntil comes first. A zero time means no delay, so it's always first.
if h[i].item.DelayedUntil.IsZero() {
return true
}
if h[j].item.DelayedUntil.IsZero() {
return false
}
return h[i].item.DelayedUntil.Before(h[j].item.DelayedUntil)
}
func (h delayHeap[K, V]) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
h[i].idxDelay = i
h[j].idxDelay = j
}
func (h *delayHeap[K, V]) Push(x interface{}) {
n := x.(*node[K, V])
n.idxDelay = len(*h)
*h = append(*h, n)
}
func (h *delayHeap[K, V]) Pop() interface{} {
old := *h
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.idxDelay = -1
*h = old[0 : n-1]
return item
}