mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-04 12:59:23 +08:00
a88c896733
https://github.com/milvus-io/milvus/issues/31007 --------- Signed-off-by: longjiquan <jiquan.long@zilliz.com>
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package connection
|
|
|
|
import (
|
|
"container/heap"
|
|
"time"
|
|
)
|
|
|
|
type queueItem struct {
|
|
identifier int64
|
|
lastActiveTime time.Time
|
|
}
|
|
|
|
func newQueryItem(identifier int64, lastActiveTime time.Time) *queueItem {
|
|
return &queueItem{
|
|
identifier: identifier,
|
|
lastActiveTime: lastActiveTime,
|
|
}
|
|
}
|
|
|
|
type priorityQueue []*queueItem
|
|
|
|
func (pq priorityQueue) Len() int {
|
|
return len(pq)
|
|
}
|
|
|
|
func (pq priorityQueue) Less(i, j int) bool {
|
|
// we should purge the oldest, so the newest should be on the root.
|
|
return pq[i].lastActiveTime.After(pq[j].lastActiveTime)
|
|
}
|
|
|
|
func (pq priorityQueue) Swap(i, j int) {
|
|
pq[i], pq[j] = pq[j], pq[i]
|
|
}
|
|
|
|
func (pq *priorityQueue) Push(x interface{}) {
|
|
item := x.(*queueItem)
|
|
*pq = append(*pq, item)
|
|
}
|
|
|
|
func (pq *priorityQueue) Pop() interface{} {
|
|
old := *pq
|
|
n := len(old)
|
|
item := old[n-1]
|
|
*pq = old[:n-1]
|
|
return item
|
|
}
|
|
|
|
func newPriorityQueueWithCap(cap int) priorityQueue {
|
|
q := make(priorityQueue, 0, cap)
|
|
heap.Init(&q)
|
|
return q
|
|
}
|
|
|
|
func newPriorityQueue() priorityQueue {
|
|
return newPriorityQueueWithCap(0)
|
|
}
|