From edc78e2bb7f2ad1aab86c4e6876d4b01e56fbcb6 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Mon, 17 Oct 2016 20:32:58 +0200 Subject: [PATCH] parallelism --- internal/exec/exec.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/exec/exec.go b/internal/exec/exec.go index 4f335893b1f..42e70ed9ceb 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -3,6 +3,7 @@ package exec import ( "reflect" "strings" + "sync" "github.com/neelance/graphql-go/internal/query" "github.com/neelance/graphql-go/internal/schema" @@ -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 } @@ -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 }