Commit be9e1d21 authored by Jeromy's avatar Jeromy

make FetchGraph waitable

parent 03fab00c
...@@ -2,6 +2,7 @@ package merkledag ...@@ -2,6 +2,7 @@ package merkledag
import ( import (
"fmt" "fmt"
"sync"
"time" "time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...@@ -242,9 +243,16 @@ func (n *dagService) Remove(nd *Node) error { ...@@ -242,9 +243,16 @@ func (n *dagService) Remove(nd *Node) error {
return n.Blocks.DeleteBlock(k) return n.Blocks.DeleteBlock(k)
} }
func FetchGraph(ctx context.Context, root *Node, serv DAGService) { func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{} {
var wg sync.WaitGroup
done := make(chan struct{})
for _, l := range root.Links { for _, l := range root.Links {
wg.Add(1)
go func(lnk *Link) { go func(lnk *Link) {
// Signal child is done on way out
defer wg.Done()
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
...@@ -255,7 +263,16 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) { ...@@ -255,7 +263,16 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) {
log.Error(err) log.Error(err)
return return
} }
FetchGraph(ctx, nd, serv)
// Wait for children to finish
<-FetchGraph(ctx, nd, serv)
}(l) }(l)
} }
go func() {
wg.Wait()
done <- struct{}{}
}()
return done
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment