Skip to content

Commit

Permalink
parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 17, 2016
1 parent fb63371 commit edc78e2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"reflect"
"strings"
"sync"

"github.com/neelance/graphql-go/internal/query"
"github.com/neelance/graphql-go/internal/schema"
Expand Down Expand Up @@ -101,9 +102,15 @@ type listExec struct {

func (e *listExec) exec(r *request, selSet *query.SelectionSet, resolver reflect.Value) interface{} {
l := make([]interface{}, resolver.Len())
var wg sync.WaitGroup
for i := range l {
l[i] = e.elem.exec(r, selSet, resolver.Index(i))
wg.Add(1)
go func(i int) {
l[i] = e.elem.exec(r, selSet, resolver.Index(i))
wg.Done()
}(i)
}
wg.Wait()
return l
}

Expand All @@ -121,12 +128,23 @@ type objectExec struct {
}

func (e *objectExec) exec(r *request, selSet *query.SelectionSet, resolver reflect.Value) interface{} {
result := make(map[string]interface{})
var allFields []*query.Field
collectFields(r, selSet, &allFields)

var wg sync.WaitGroup
var m sync.Mutex
result := make(map[string]interface{})
for _, f := range allFields {
result[f.Alias] = e.fields[f.Name].exec(r, f, resolver)
wg.Add(1)
go func(f *query.Field) {
v := e.fields[f.Name].exec(r, f, resolver)
m.Lock()
result[f.Alias] = v
m.Unlock()
wg.Done()
}(f)
}
wg.Wait()
return result
}

Expand Down

0 comments on commit edc78e2

Please sign in to comment.