-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#31007 pr: #31037 --------- Signed-off-by: longjiquan <[email protected]>
- Loading branch information
1 parent
542b46f
commit c37b779
Showing
6 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package connection | ||
|
||
import ( | ||
"container/heap" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_priorityQueue(t *testing.T) { | ||
q := newPriorityQueue() | ||
repeat := 10 | ||
for i := 0; i < repeat; i++ { | ||
heap.Push(&q, newQueryItem(int64(i), time.Now())) | ||
} | ||
counter := repeat - 1 | ||
for q.Len() > 0 { | ||
item := heap.Pop(&q).(*queueItem) | ||
assert.Equal(t, int64(counter), item.identifier) | ||
counter-- | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters