延时队列增加数量获取功能

This commit is contained in:
fantasticbin 2024-12-10 18:14:04 +08:00
parent 28086ea8ee
commit 899c206c03
2 changed files with 12 additions and 12 deletions

View File

@ -115,6 +115,14 @@ func (q *Queues[TKey, TValue, TRoute]) Len(route TRoute) int {
return 0
}
// DelayCount 获取延时队列数量
func (q *Queues[TKey, TValue, TRoute]) DelayCount(route TRoute) uint64 {
if queue, ok := q.queues.Load(route); ok {
return queue.(*DelayLkQueue[TKey, TValue]).DelayCount()
}
return 0
}
// DelayEnqueue 延迟入队
func (q *Queues[TKey, TValue, TRoute]) DelayEnqueue(route TRoute, value TValue, duration time.Duration) {
// 入队时检查上一次出队是否确认成功

View File

@ -16,22 +16,14 @@ func TestQueues(t *testing.T) {
route := "test"
q := NewQueues[struct{}, int, string]()
for _, c := range cases {
q.Enqueue(route, c.value)
}
if q.Len(route) != len(cases) {
t.Errorf("queue length error, want %d, got %d", len(cases), q.Len(route))
}
for range cases {
q.Dequeue(route)
}
for _, c := range cases {
q.DelayEnqueue(route, c.value, c.duration)
}
if q.DelayCount(route) != uint64(len(cases)) {
t.Errorf("queue length error, want %d, got %d", len(cases), q.DelayCount(route))
}
notify := make(chan int)
go func() {
for data := range notify {