1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-09-16 05:50:41 +02:00

close the input

This commit is contained in:
chrislu 2024-01-27 16:12:49 -08:00
parent fa835c9319
commit 5dc1362bdc

View file

@ -1,6 +1,7 @@
package buffered_queue package buffered_queue
import ( import (
"fmt"
"sync" "sync"
) )
@ -24,6 +25,7 @@ type BufferedQueue[T any] struct {
nodeCounter int nodeCounter int
waitOnRead bool waitOnRead bool
waitCond *sync.Cond waitCond *sync.Cond
isClosed bool
} }
// NewBufferedQueue creates a new buffered queue with the specified chunk size // NewBufferedQueue creates a new buffered queue with the specified chunk size
@ -44,7 +46,12 @@ func NewBufferedQueue[T any](chunkSize int, waitOnRead bool) *BufferedQueue[T] {
} }
// Enqueue adds a job to the queue // Enqueue adds a job to the queue
func (q *BufferedQueue[T]) Enqueue(job T) { func (q *BufferedQueue[T]) Enqueue(job T) error {
if q.isClosed {
return fmt.Errorf("queue is closed")
}
q.mutex.Lock() q.mutex.Lock()
defer q.mutex.Unlock() defer q.mutex.Unlock()
@ -73,6 +80,8 @@ func (q *BufferedQueue[T]) Enqueue(job T) {
if q.waitOnRead { if q.waitOnRead {
q.waitCond.Signal() q.waitCond.Signal()
} }
return nil
} }
// Dequeue removes and returns a job from the queue // Dequeue removes and returns a job from the queue
@ -81,9 +90,13 @@ func (q *BufferedQueue[T]) Dequeue() (T, bool) {
defer q.mutex.Unlock() defer q.mutex.Unlock()
if q.waitOnRead { if q.waitOnRead {
for q.count <= 0 { for q.count <= 0 && !q.isClosed {
q.waitCond.Wait() q.waitCond.Wait()
} }
if q.isClosed {
var a T
return a, false
}
} else { } else {
if q.count == 0 { if q.count == 0 {
var a T var a T
@ -124,3 +137,10 @@ func (q *BufferedQueue[T]) Size() int {
func (q *BufferedQueue[T]) IsEmpty() bool { func (q *BufferedQueue[T]) IsEmpty() bool {
return q.Size() == 0 return q.Size() == 0
} }
func (q *BufferedQueue[T]) CloseInput() {
q.mutex.Lock()
defer q.mutex.Unlock()
q.isClosed = true
q.waitCond.Broadcast()
}