Skip to content

Commit

Permalink
Merge pull request #20 from andreas/experiment-parallel-resolve
Browse files Browse the repository at this point in the history
Evaluate list fields in parallel
  • Loading branch information
sogko authored Jun 11, 2016
2 parents 2dcd77e + fdd0e5b commit 6414283
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,13 +820,34 @@ func completeListValue(eCtx *ExecutionContext, returnType *List, fieldASTs []*as
panic(gqlerrors.FormatError(err))
}

// concurrently resolve list elements
itemType := returnType.OfType
completedResults := []interface{}{}
wg := sync.WaitGroup{}
completedResults := make([]interface{}, resultVal.Len())
panics := make(chan interface{}, resultVal.Len())
for i := 0; i < resultVal.Len(); i++ {
val := resultVal.Index(i).Interface()
completedItem := completeValueCatchingError(eCtx, itemType, fieldASTs, info, val)
completedResults = append(completedResults, completedItem)
wg.Add(1)
go func(j int) {
defer func() {
if r := recover(); r != nil {
panics <- r
}
wg.Done()
}()
val := resultVal.Index(j).Interface()
completedResults[j] = completeValueCatchingError(eCtx, itemType, fieldASTs, info, val)
}(i)
}

// wait for all routines to complete and then perform clean up
wg.Wait()
close(panics)

// re-panic if a goroutine panicked
for p := range panics {
panic(p)
}

return completedResults
}

Expand Down

0 comments on commit 6414283

Please sign in to comment.