From 34aa10f9b8586d46fec3b3a486b268e81007d0f7 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 1 Dec 2017 00:35:33 +0000 Subject: [PATCH] Pass channels into goroutines to prevent data race --- db/iterator.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/db/iterator.go b/db/iterator.go index 823fe87d..9d74bee6 100644 --- a/db/iterator.go +++ b/db/iterator.go @@ -236,24 +236,24 @@ func (i *iterator) setupWorkers(ctx context.Context) { switch { case i.tasks == 0: for w := 1; w <= workerCount; w++ { - go i.setupWorker(ctx) + go i.setupWorker(ctx, i.jobs, i.vals) } default: for w := 1; w <= ints.Between(1, workerCount, i.tasks); w++ { - go i.setupWorker(ctx) + go i.setupWorker(ctx, i.jobs, i.vals) } } } } -func (i *iterator) setupWorker(ctx context.Context) { +func (i *iterator) setupWorker(ctx context.Context, jobs chan *workable, vals chan *doneable) { - for j := range i.jobs { + for j := range jobs { res, err := newDocument(i, j.key, j.val, j.doc).query(ctx, i.stm) - i.vals <- &doneable{res: res, err: err} + vals <- &doneable{res: res, err: err} } @@ -269,17 +269,17 @@ func (i *iterator) submitTask(key *keys.Thing, val kvs.KV, doc *data.Doc) { func (i *iterator) checkWorker(ctx context.Context) { - go func() { - for err := range i.fail { + go func(fail chan error) { + for err := range fail { i.receivedError(err) } - }() + }(i.fail) - go func() { - for val := range i.vals { + go func(vals chan *doneable) { + for val := range vals { i.receivedResult(val) } - }() + }(i.vals) }