mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
make FetchGraph waitable
This commit is contained in:
@ -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 {
|
|||||||
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) {
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user