2024-12-09 10:50:31 +08:00
|
|
|
package lock_free
|
|
|
|
|
|
|
|
import (
|
|
|
|
"iter"
|
2024-12-09 20:35:24 +08:00
|
|
|
"sync"
|
2024-12-09 10:50:31 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Queues 队列集合
|
2024-12-09 22:25:50 +08:00
|
|
|
type Queues[TKey comparable, TValue, TRoute any] struct {
|
|
|
|
queues sync.Map // 读多写少的场景,适合用 sync.Map 路由队列
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueues 创建队列集合
|
2024-12-09 22:25:50 +08:00
|
|
|
func NewQueues[TKey comparable, TValue, TRoute any]() *Queues[TKey, TValue, TRoute] {
|
|
|
|
return &Queues[TKey, TValue, TRoute]{}
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue 入队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) Enqueue(route TRoute, value TValue) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).Enqueue(value)
|
2024-12-09 10:50:31 +08:00
|
|
|
} else {
|
2024-12-09 22:25:50 +08:00
|
|
|
queue := NewDelayLkQueue[TKey, TValue]()
|
|
|
|
q.queues.Store(route, queue)
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.Enqueue(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue 出队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) Dequeue(route TRoute) (value TValue, ok bool) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
return queue.(*DelayLkQueue[TKey, TValue]).Dequeue()
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
return value, false
|
|
|
|
}
|
|
|
|
|
2024-12-10 08:53:30 +08:00
|
|
|
// Len 队列长度
|
|
|
|
func (q *Queues[TKey, TValue, TRoute]) Len(route TRoute) int {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
return queue.(*DelayLkQueue[TKey, TValue]).Len()
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-12-09 10:50:31 +08:00
|
|
|
// DelayEnqueue 延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) DelayEnqueue(route TRoute, value TValue, duration time.Duration) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).DelayEnqueue(value, duration)
|
2024-12-09 10:50:31 +08:00
|
|
|
} else {
|
2024-12-09 22:25:50 +08:00
|
|
|
queue := NewDelayLkQueue[TKey, TValue]()
|
|
|
|
q.queues.Store(route, queue)
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.DelayEnqueue(value, duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancellableDelayEnqueue 可取消的延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) CancellableDelayEnqueue(route TRoute, key TKey, value TValue, duration time.Duration) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).CancellableDelayEnqueue(key, value, duration)
|
2024-12-09 10:50:31 +08:00
|
|
|
} else {
|
2024-12-09 22:25:50 +08:00
|
|
|
queue := NewDelayLkQueue[TKey, TValue]()
|
|
|
|
q.queues.Store(route, queue)
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.CancellableDelayEnqueue(key, value, duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelDelayEnqueue 取消延迟入队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) CancelDelayEnqueue(route TRoute, key TKey) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).CancelDelayEnqueue(key)
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeue 持续监听出队
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) ContinuousDequeue(route TRoute) iter.Seq[TValue] {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
return queue.(*DelayLkQueue[TKey, TValue]).ContinuousDequeue()
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeueExecute 持续监听出队执行函数
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) ContinuousDequeueExecute(route TRoute, fn func(TValue)) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).ContinuousDequeueExecute(fn)
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeueNotify 持续监听出队通知
|
2024-12-09 22:25:50 +08:00
|
|
|
func (q *Queues[TKey, TValue, TRoute]) ContinuousDequeueNotify(route TRoute, chs ...chan TValue) {
|
|
|
|
if queue, ok := q.queues.Load(route); ok {
|
|
|
|
queue.(*DelayLkQueue[TKey, TValue]).ContinuousDequeueNotify(chs...)
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
}
|