mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-08 23:50:14 +08:00
update: use oarkflow/json
This commit is contained in:
11
queue.go
11
queue.go
@@ -45,38 +45,33 @@ func (b *Broker) NewQueue(name string) *Queue {
|
||||
type QueueTask struct {
|
||||
ctx context.Context
|
||||
payload *Task
|
||||
retryCount int
|
||||
priority int
|
||||
index int // The index in the heap
|
||||
retryCount int
|
||||
index int
|
||||
}
|
||||
|
||||
type PriorityQueue []*QueueTask
|
||||
|
||||
func (pq PriorityQueue) Len() int { return len(pq) }
|
||||
|
||||
func (pq PriorityQueue) Less(i, j int) bool {
|
||||
return pq[i].priority > pq[j].priority
|
||||
}
|
||||
|
||||
func (pq PriorityQueue) Swap(i, j int) {
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
pq[i].index = i
|
||||
pq[j].index = j
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Push(x interface{}) {
|
||||
n := len(*pq)
|
||||
task := x.(*QueueTask)
|
||||
task.index = n
|
||||
*pq = append(*pq, task)
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Pop() interface{} {
|
||||
old := *pq
|
||||
n := len(old)
|
||||
task := old[n-1]
|
||||
old[n-1] = nil // avoid memory leak
|
||||
task.index = -1 // for safety
|
||||
task.index = -1
|
||||
*pq = old[0 : n-1]
|
||||
return task
|
||||
}
|
||||
|
Reference in New Issue
Block a user