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 队列集合
|
|
|
|
type Queues[TValue any, TKey, TRoute comparable] struct {
|
|
|
|
queues map[TRoute]*DelayLkQueue[TValue, TKey]
|
2024-12-09 20:35:24 +08:00
|
|
|
m sync.RWMutex
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueues 创建队列集合
|
|
|
|
func NewQueues[TValue any, TKey, TRoute comparable]() *Queues[TValue, TKey, TRoute] {
|
2024-12-09 20:35:24 +08:00
|
|
|
return &Queues[TValue, TKey, TRoute]{make(map[TRoute]*DelayLkQueue[TValue, TKey]), sync.RWMutex{}}
|
2024-12-09 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue 入队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) Enqueue(route TRoute, value TValue) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.Enqueue(value)
|
|
|
|
} else {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue := NewDelayLkQueue[TValue, TKey]()
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Lock()
|
2024-12-09 10:50:31 +08:00
|
|
|
q.queues[route] = queue
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Unlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.Enqueue(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue 出队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) Dequeue(route TRoute) (value TValue, ok bool) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
return queue.Dequeue()
|
|
|
|
}
|
|
|
|
return value, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelayEnqueue 延迟入队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) DelayEnqueue(route TRoute, value TValue, duration time.Duration) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.DelayEnqueue(value, duration)
|
|
|
|
} else {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue := NewDelayLkQueue[TValue, TKey]()
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Lock()
|
2024-12-09 10:50:31 +08:00
|
|
|
q.queues[route] = queue
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Unlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.DelayEnqueue(value, duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancellableDelayEnqueue 可取消的延迟入队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) CancellableDelayEnqueue(route TRoute, key TKey, value TValue, duration time.Duration) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
queue.CancellableDelayEnqueue(key, value, duration)
|
|
|
|
} else {
|
|
|
|
queue := NewDelayLkQueue[TValue, TKey]()
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Lock()
|
2024-12-09 10:50:31 +08:00
|
|
|
q.queues[route] = queue
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.Unlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
queue.CancellableDelayEnqueue(key, value, duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelDelayEnqueue 取消延迟入队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) CancelDelayEnqueue(route TRoute, key TKey) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
queue.CancelDelayEnqueue(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeue 持续监听出队
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) ContinuousDequeue(route TRoute) iter.Seq[TValue] {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
return queue.ContinuousDequeue()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeueExecute 持续监听出队执行函数
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) ContinuousDequeueExecute(route TRoute, fn func(TValue)) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
queue.ContinuousDequeueExecute(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinuousDequeueNotify 持续监听出队通知
|
|
|
|
func (q *Queues[TValue, TKey, TRoute]) ContinuousDequeueNotify(route TRoute, chs ...chan TValue) {
|
2024-12-09 20:35:24 +08:00
|
|
|
q.m.RLock()
|
|
|
|
defer q.m.RUnlock()
|
2024-12-09 10:50:31 +08:00
|
|
|
if queue, ok := q.queues[route]; ok {
|
|
|
|
queue.ContinuousDequeueNotify(chs...)
|
|
|
|
}
|
|
|
|
}
|