Skip to content

Commit

Permalink
rework FetchGraph to be less of a memory hog
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Feb 20, 2016
1 parent 479761e commit 5806ac0
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,18 @@ func (ds *dagService) GetMany(ctx context.Context, keys []key.Key) (<-chan *Node
out := make(chan *Node)
errs := make(chan error, 1)
blocks := ds.Blocks.GetBlocks(ctx, keys)
var count int

go func() {
defer close(out)
defer close(errs)
for {
select {
case b, ok := <-blocks:
if !ok {
if count != len(keys) {
errs <- fmt.Errorf("failed to fetch all nodes")
}
return
}
nd, err := Decoded(b.Data)
Expand All @@ -165,6 +170,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []key.Key) (<-chan *Node
}
select {
case out <- nd:
count++
case <-ctx.Done():
return
}
Expand Down Expand Up @@ -404,28 +410,27 @@ func EnumerateChildrenAsync(ctx context.Context, ds DAGService, root *Node, set
func fetchNodes(ctx context.Context, ds DAGService, in <-chan []key.Key, out chan<- *Node, errs chan<- error) {
defer close(out)

get := func(g NodeGetter) {
nd, err := g.Get(ctx)
if err != nil {
get := func(ks []key.Key) {
nodes, errch := ds.GetMany(ctx, ks)
for {
select {
case errs <- err:
case <-ctx.Done():
case nd, ok := <-nodes:
if !ok {
return
}
select {
case out <- nd:
case <-ctx.Done():
return
}
case err := <-errch:
errs <- err
return
}
return
}

select {
case out <- nd:
case <-ctx.Done():
return
}
}

for ks := range in {
ng := GetNodes(ctx, ds, ks)
for _, g := range ng {
go get(g)
}
go get(ks)
}

}

0 comments on commit 5806ac0

Please sign in to comment.