35 lines
760 B
Go
35 lines
760 B
Go
package lock_free
|
|
|
|
import "time"
|
|
|
|
type DelayLkQueue[T any] struct {
|
|
gap time.Duration // 控制监听重试间隔
|
|
LkQueue[T]
|
|
}
|
|
|
|
// NewDelayLkQueue 创建延迟无锁队列
|
|
func NewDelayLkQueue[T any](opt ...Option) *DelayLkQueue[T] {
|
|
opts := loadOptions(opt...)
|
|
return &DelayLkQueue[T]{opts.gap, *NewLkQueue[T]()}
|
|
}
|
|
|
|
// DelayEnqueue 延迟入队
|
|
func (q *DelayLkQueue[T]) DelayEnqueue(value T, duration time.Duration) {
|
|
time.AfterFunc(duration, func() {
|
|
q.Enqueue(value)
|
|
})
|
|
}
|
|
|
|
// ContinuousDequeue 持续监听出队通知
|
|
func (q *DelayLkQueue[T]) ContinuousDequeue(notify ...chan T) {
|
|
for {
|
|
if value, ok := q.Dequeue(); ok {
|
|
for _, n := range notify {
|
|
n <- value
|
|
}
|
|
} else {
|
|
time.Sleep(q.gap) // 队列为空,休眠重试
|
|
}
|
|
}
|
|
}
|