|
| 1 | +package chanutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/lightninglabs/neutrino/cache/lru" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + // DefaultQueueSize is the default size to use for concurrent queues. |
| 11 | + DefaultQueueSize = 10 |
| 12 | +) |
| 13 | + |
| 14 | +// ConcurrentQueue is a typed concurrent-safe FIFO queue with unbounded |
| 15 | +// capacity. Clients interact with the queue by pushing items into the in |
| 16 | +// channel and popping items from the out channel. There is a goroutine that |
| 17 | +// manages moving items from the in channel to the out channel in the correct |
| 18 | +// order that must be started by calling Start(). |
| 19 | +type ConcurrentQueue[T any] struct { |
| 20 | + started sync.Once |
| 21 | + stopped sync.Once |
| 22 | + |
| 23 | + chanIn chan T |
| 24 | + chanOut chan T |
| 25 | + overflow *lru.List[T] |
| 26 | + |
| 27 | + wg sync.WaitGroup |
| 28 | + quit chan struct{} |
| 29 | +} |
| 30 | + |
| 31 | +// NewConcurrentQueue constructs a ConcurrentQueue. The bufferSize parameter is |
| 32 | +// the capacity of the output channel. When the size of the queue is below this |
| 33 | +// threshold, pushes do not incur the overhead of the less efficient overflow |
| 34 | +// structure. |
| 35 | +func NewConcurrentQueue[T any](bufferSize int) *ConcurrentQueue[T] { |
| 36 | + return &ConcurrentQueue[T]{ |
| 37 | + chanIn: make(chan T), |
| 38 | + chanOut: make(chan T, bufferSize), |
| 39 | + overflow: lru.NewList[T](), |
| 40 | + quit: make(chan struct{}), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// ChanIn returns a channel that can be used to push new items into the queue. |
| 45 | +func (cq *ConcurrentQueue[T]) ChanIn() chan<- T { |
| 46 | + return cq.chanIn |
| 47 | +} |
| 48 | + |
| 49 | +// ChanOut returns a channel that can be used to pop items from the queue. |
| 50 | +func (cq *ConcurrentQueue[T]) ChanOut() <-chan T { |
| 51 | + return cq.chanOut |
| 52 | +} |
| 53 | + |
| 54 | +// Start begins a goroutine that manages moving items from the in channel to the |
| 55 | +// out channel. The queue tries to move items directly to the out channel |
| 56 | +// minimize overhead, but if the out channel is full it pushes items to an |
| 57 | +// overflow queue. This must be called before using the queue. |
| 58 | +func (cq *ConcurrentQueue[T]) Start() { |
| 59 | + cq.started.Do(cq.start) |
| 60 | +} |
| 61 | + |
| 62 | +func (cq *ConcurrentQueue[T]) start() { |
| 63 | + cq.wg.Add(1) |
| 64 | + go func() { |
| 65 | + defer cq.wg.Done() |
| 66 | + |
| 67 | + readLoop: |
| 68 | + for { |
| 69 | + nextElement := cq.overflow.Front() |
| 70 | + if nextElement == nil { |
| 71 | + // Overflow queue is empty so incoming items can |
| 72 | + // be pushed directly to the output channel. If |
| 73 | + // output channel is full though, push to |
| 74 | + // overflow. |
| 75 | + select { |
| 76 | + case item, ok := <-cq.chanIn: |
| 77 | + if !ok { |
| 78 | + log.Warnf("ConcurrentQueue " + |
| 79 | + "has exited due to " + |
| 80 | + "the input channel " + |
| 81 | + "being closed") |
| 82 | + |
| 83 | + break readLoop |
| 84 | + } |
| 85 | + select { |
| 86 | + case cq.chanOut <- item: |
| 87 | + // Optimistically push directly |
| 88 | + // to chanOut. |
| 89 | + default: |
| 90 | + cq.overflow.PushBack(item) |
| 91 | + } |
| 92 | + case <-cq.quit: |
| 93 | + return |
| 94 | + } |
| 95 | + } else { |
| 96 | + // Overflow queue is not empty, so any new items |
| 97 | + // get pushed to the back to preserve order. |
| 98 | + select { |
| 99 | + case item, ok := <-cq.chanIn: |
| 100 | + if !ok { |
| 101 | + log.Warnf("ConcurrentQueue " + |
| 102 | + "has exited due to " + |
| 103 | + "the input channel " + |
| 104 | + "being closed") |
| 105 | + |
| 106 | + break readLoop |
| 107 | + } |
| 108 | + cq.overflow.PushBack(item) |
| 109 | + case cq.chanOut <- nextElement.Value: |
| 110 | + cq.overflow.Remove(nextElement) |
| 111 | + case <-cq.quit: |
| 112 | + return |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Incoming channel has been closed. Empty overflow queue into |
| 118 | + // the outgoing channel. |
| 119 | + nextElement := cq.overflow.Front() |
| 120 | + for nextElement != nil { |
| 121 | + select { |
| 122 | + case cq.chanOut <- nextElement.Value: |
| 123 | + cq.overflow.Remove(nextElement) |
| 124 | + case <-cq.quit: |
| 125 | + return |
| 126 | + } |
| 127 | + nextElement = cq.overflow.Front() |
| 128 | + } |
| 129 | + |
| 130 | + // Close outgoing channel. |
| 131 | + close(cq.chanOut) |
| 132 | + }() |
| 133 | +} |
| 134 | + |
| 135 | +// Stop ends the goroutine that moves items from the in channel to the out |
| 136 | +// channel. This does not clear the queue state, so the queue can be restarted |
| 137 | +// without dropping items. |
| 138 | +func (cq *ConcurrentQueue[T]) Stop() { |
| 139 | + cq.stopped.Do(func() { |
| 140 | + close(cq.quit) |
| 141 | + cq.wg.Wait() |
| 142 | + }) |
| 143 | +} |
0 commit comments