lotus/request_queue.go

37 lines
682 B
Go
Raw Normal View History

2020-05-07 23:38:05 +00:00
package sectorstorage
import "container/heap"
type requestQueue []*workerRequest
func (q requestQueue) Len() int { return len(q) }
func (q requestQueue) Less(i, j int) bool {
return q[i].taskType.Less(q[j].taskType)
}
func (q requestQueue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
q[i].index = i
q[j].index = j
}
func (q *requestQueue) Push(x interface{}) {
n := len(*q)
item := x.(*workerRequest)
item.index = n
*q = append(*q, item)
}
func (q *requestQueue) Pop() interface{} {
old := *q
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*q = old[0 : n-1]
return item
}
var _ heap.Interface = &requestQueue{}