2024-12-03 10:10:02 +08:00
|
|
|
|
package lock_free
|
|
|
|
|
|
2024-12-03 13:01:11 +08:00
|
|
|
|
import (
|
|
|
|
|
"iter"
|
2024-12-09 20:35:24 +08:00
|
|
|
|
"sync"
|
2024-12-10 17:38:01 +08:00
|
|
|
|
"sync/atomic"
|
2024-12-03 13:01:11 +08:00
|
|
|
|
"time"
|
|
|
|
|
)
|
2024-12-03 10:10:02 +08:00
|
|
|
|
|
2024-12-09 22:25:50 +08:00
|
|
|
|
type DelayLkQueue[TKey comparable, TValue any] struct {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
timers map[TKey]*time.Timer
|
|
|
|
|
delayCount atomic.Uint64 // 用于统计延时队列数量
|
|
|
|
|
m sync.Mutex
|
2024-12-09 10:50:31 +08:00
|
|
|
|
LkQueue[TValue]
|
2024-12-03 10:10:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 10:25:34 +08:00
|
|
|
|
// NewDelayLkQueue 创建延迟无锁队列
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func NewDelayLkQueue[TKey comparable, TValue any]() *DelayLkQueue[TKey, TValue] {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
return &DelayLkQueue[TKey, TValue]{
|
|
|
|
|
make(map[TKey]*time.Timer),
|
|
|
|
|
atomic.Uint64{},
|
|
|
|
|
sync.Mutex{},
|
|
|
|
|
*NewLkQueue[TValue](),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DelayCount 获取延时队列数量
|
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) DelayCount() uint64 {
|
|
|
|
|
return q.delayCount.Load()
|
2024-12-03 10:25:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 10:10:02 +08:00
|
|
|
|
// DelayEnqueue 延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) DelayEnqueue(value TValue, duration time.Duration) {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
q.delayCount.Add(1)
|
2024-12-03 10:10:02 +08:00
|
|
|
|
time.AfterFunc(duration, func() {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
q.delayCount.Add(^uint64(0))
|
2024-12-03 10:10:02 +08:00
|
|
|
|
q.Enqueue(value)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 10:50:31 +08:00
|
|
|
|
// CancellableDelayEnqueue 可取消的延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) CancellableDelayEnqueue(key TKey, value TValue, duration time.Duration) {
|
2024-12-10 08:10:08 +08:00
|
|
|
|
q.m.Lock()
|
|
|
|
|
defer q.m.Unlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
|
if timer, ok := q.timers[key]; ok {
|
|
|
|
|
timer.Stop()
|
|
|
|
|
}
|
2024-12-10 17:38:01 +08:00
|
|
|
|
q.delayCount.Add(1)
|
2024-12-09 10:50:31 +08:00
|
|
|
|
q.timers[key] = time.AfterFunc(duration, func() {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
q.delayCount.Add(^uint64(0))
|
2024-12-09 10:50:31 +08:00
|
|
|
|
delete(q.timers, key)
|
|
|
|
|
q.Enqueue(value)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CancelDelayEnqueue 取消延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) CancelDelayEnqueue(key TKey) {
|
2024-12-09 20:35:24 +08:00
|
|
|
|
q.m.Lock()
|
|
|
|
|
defer q.m.Unlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
|
if timer, ok := q.timers[key]; ok {
|
2024-12-10 17:38:01 +08:00
|
|
|
|
q.delayCount.Add(^uint64(0))
|
2024-12-09 10:50:31 +08:00
|
|
|
|
delete(q.timers, key)
|
|
|
|
|
timer.Stop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 13:01:11 +08:00
|
|
|
|
// ContinuousDequeue 持续监听出队
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) ContinuousDequeue() iter.Seq[TValue] {
|
2024-12-09 10:50:31 +08:00
|
|
|
|
return func(yield func(TValue) bool) {
|
2024-12-03 13:01:11 +08:00
|
|
|
|
for {
|
|
|
|
|
if value, ok := q.Dequeue(); ok {
|
|
|
|
|
if !yield(value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
time.Sleep(time.Millisecond) // 队列为空,休眠1毫秒
|
2024-12-03 10:32:08 +08:00
|
|
|
|
}
|
2024-12-03 10:10:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-03 13:01:11 +08:00
|
|
|
|
|
|
|
|
|
// ContinuousDequeueExecute 持续监听出队执行函数
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) ContinuousDequeueExecute(fn func(TValue)) {
|
2024-12-03 13:01:11 +08:00
|
|
|
|
for value := range q.ContinuousDequeue() {
|
|
|
|
|
fn(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ContinuousDequeueNotify 持续监听出队通知
|
2024-12-09 22:25:50 +08:00
|
|
|
|
func (q *DelayLkQueue[TKey, TValue]) ContinuousDequeueNotify(chs ...chan TValue) {
|
2024-12-09 10:50:31 +08:00
|
|
|
|
q.ContinuousDequeueExecute(func(value TValue) {
|
2024-12-03 13:01:11 +08:00
|
|
|
|
for _, ch := range chs {
|
|
|
|
|
ch <- value
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|