Skip to content

Commit

Permalink
merkledag: add a concurrency limit to merkledag fetch graph
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Dec 10, 2016
1 parent b6c1155 commit 0c14b41
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ func EnumerateChildrenAsync(ctx context.Context, ds DAGService, c *cid.Cid, visi
}
}

// FetchGraphConcurrency is total number of concurrenct fetches that
// 'fetchNodes' will start at a time
var FetchGraphConcurrency = 8

func fetchNodes(ctx context.Context, ds DAGService, in <-chan []*cid.Cid, out chan<- *NodeOption) {
var wg sync.WaitGroup
defer func() {
Expand All @@ -458,8 +462,13 @@ func fetchNodes(ctx context.Context, ds DAGService, in <-chan []*cid.Cid, out ch
close(out)
}()

rateLimit := make(chan struct{}, FetchGraphConcurrency)

get := func(ks []*cid.Cid) {
defer wg.Done()
defer func() {
<-rateLimit
}()
nodes := ds.GetMany(ctx, ks)
for opt := range nodes {
select {
Expand All @@ -471,6 +480,11 @@ func fetchNodes(ctx context.Context, ds DAGService, in <-chan []*cid.Cid, out ch
}

for ks := range in {
select {
case rateLimit <- struct{}{}:
case <-ctx.Done():
return
}
wg.Add(1)
go get(ks)
}
Expand Down

0 comments on commit 0c14b41

Please sign in to comment.