Skip to content

Commit

Permalink
Use cancelable context instead of cancel channel
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 5, 2019
1 parent 723bb8b commit 29bf7bb
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions provider/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Queue struct {
ds datastore.Datastore // Must be threadsafe
dequeue chan cid.Cid
enqueue chan cid.Cid
close chan struct{}
close context.CancelFunc
}

// NewQueue creates a queue for cids
Expand All @@ -37,26 +37,24 @@ func NewQueue(ctx context.Context, name string, ds datastore.Datastore) (*Queue,
if err != nil {
return nil, err
}
cancelCtx, cancel := context.WithCancel(ctx)
q := &Queue{
name: name,
ctx: ctx,
ctx: cancelCtx,
head: head,
tail: tail,
ds: namespaced,
dequeue: make(chan cid.Cid),
enqueue: make(chan cid.Cid),
close: make(chan struct{}),
close: cancel,
}
q.work()
return q, nil
}

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

Expand Down Expand Up @@ -145,8 +143,6 @@ func (q *Queue) work() {
q.head++
case <-q.ctx.Done():
return
case <-q.close:
return
}
}
}()
Expand Down

0 comments on commit 29bf7bb

Please sign in to comment.