Skip to content

Commit c5bdf3b

Browse files
authored
Merge pull request #400 from sourcegraph/limit-goroutines
Limit the number of concurrent list nodes processed
2 parents 3b5ddcd + 4659b07 commit c5bdf3b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

internal/exec/exec.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,21 @@ func (r *Request) execList(ctx context.Context, sels []selected.Selection, typ *
313313
entryouts := make([]bytes.Buffer, l)
314314

315315
if selected.HasAsyncSel(sels) {
316-
var wg sync.WaitGroup
317-
wg.Add(l)
316+
// Limit the number of concurrent goroutines spawned as it can lead to large
317+
// memory spikes for large lists.
318+
concurrency := cap(r.Limiter)
319+
sem := make(chan struct{}, concurrency)
318320
for i := 0; i < l; i++ {
321+
sem <- struct{}{}
319322
go func(i int) {
320-
defer wg.Done()
323+
defer func() { <-sem }()
321324
defer r.handlePanic(ctx)
322325
r.execSelectionSet(ctx, sels, typ.OfType, &pathSegment{path, i}, s, resolver.Index(i), &entryouts[i])
323326
}(i)
324327
}
325-
wg.Wait()
328+
for i := 0; i < concurrency;i++ {
329+
sem <- struct{}{}
330+
}
326331
} else {
327332
for i := 0; i < l; i++ {
328333
r.execSelectionSet(ctx, sels, typ.OfType, &pathSegment{path, i}, s, resolver.Index(i), &entryouts[i])

0 commit comments

Comments
 (0)