Skip to content

Commit

Permalink
Only allow close to happen once
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Michael Avila <davidmichaelavila@gmail.com>
  • Loading branch information
michaelavila committed Apr 8, 2019
1 parent 59ef6e3 commit 67653f6
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions provider/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ func NewQueue(ctx context.Context, name string, ds datastore.Datastore) (*Queue,
dequeue: make(chan cid.Cid),
enqueue: make(chan cid.Cid),
close: make(chan struct{}),
done: make(chan struct{}),
done: make(chan struct{}, 1),
}
q.work()
return q, nil
}

// Close stops the queue
func (q *Queue) Close() error {
q.close <- struct{}{}
<-q.done
return nil
for {
select {
case q.close <- struct{}{}:
case <-q.done:
return nil
}
}
}

// Enqueue puts a cid in the queue
Expand Down Expand Up @@ -113,6 +117,10 @@ func (q *Queue) work() {
var k datastore.Key = datastore.Key{}
var c cid.Cid = cid.Undef

defer func() {
q.done <- struct{}{}
}()

for {
if c == cid.Undef {
k, c = q.nextEntry()
Expand Down Expand Up @@ -144,10 +152,8 @@ func (q *Queue) work() {
c = cid.Undef
q.head++
case <-q.ctx.Done():
q.done <- struct{}{}
return
case <-q.close:
q.done <- struct{}{}
return
}
}
Expand Down

0 comments on commit 67653f6

Please sign in to comment.