Skip to content

Commit a8d0d00

Browse files
adding waitgroups to handle race
1 parent e96c7c7 commit a8d0d00

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

subscription.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graphql
33
import (
44
"context"
55
"fmt"
6+
"sync"
67

78
"github.com/graphql-go/graphql/gqlerrors"
89
"github.com/graphql-go/graphql/language/ast"
@@ -12,6 +13,7 @@ type ResultIteratorFn func(count int64, result *Result, doneFunc func())
1213

1314
type ResultIterator struct {
1415
count int64
16+
wg sync.WaitGroup
1517
ctx context.Context
1618
ch chan *Result
1719
cancelFunc context.CancelFunc
@@ -43,8 +45,12 @@ func NewResultIterator(ctx context.Context, ch chan *Result) *ResultIterator {
4345
if iterator.cancelled {
4446
return
4547
}
46-
iterator.count += 1
48+
iterator.wg.Wait()
49+
iterator.wg.Add(1)
50+
iterator.count++
51+
iterator.wg.Done()
4752
for _, handler := range iterator.handlers {
53+
iterator.wg.Wait()
4854
handler(iterator.count, res, iterator.Done)
4955
}
5056
}
@@ -55,7 +61,9 @@ func NewResultIterator(ctx context.Context, ch chan *Result) *ResultIterator {
5561
}
5662

5763
func (c *ResultIterator) ForEach(handler ResultIteratorFn) {
64+
c.wg.Add(1)
5865
c.handlers = append(c.handlers, handler)
66+
c.wg.Done()
5967
}
6068

6169
func (c *ResultIterator) Done() {

0 commit comments

Comments
 (0)