Skip to content

Commit

Permalink
Merge pull request #3756 from ipfs/fix/limit-mdag-batch-objs
Browse files Browse the repository at this point in the history
Limit number of objects in mdag batch to prevent too many fds issue
  • Loading branch information
whyrusleeping authored Mar 7, 2017
2 parents e3d483f + 31af335 commit 88af4b7
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ func (n *dagService) Add(nd node.Node) (*cid.Cid, error) {
}

func (n *dagService) Batch() *Batch {
return &Batch{ds: n, MaxSize: 8 * 1024 * 1024}
return &Batch{
ds: n,
MaxSize: 8 << 20,

// By default, only batch up to 128 nodes at a time.
// The current implementation of flatfs opens this many file
// descriptors at the same time for the optimized batch write.
MaxBlocks: 128,
}
}

// Get retrieves a node from the dagService, fetching the block in the BlockService
Expand Down Expand Up @@ -376,15 +384,16 @@ func (np *nodePromise) Get(ctx context.Context) (node.Node, error) {
type Batch struct {
ds *dagService

blocks []blocks.Block
size int
MaxSize int
blocks []blocks.Block
size int
MaxSize int
MaxBlocks int
}

func (t *Batch) Add(nd node.Node) (*cid.Cid, error) {
t.blocks = append(t.blocks, nd)
t.size += len(nd.RawData())
if t.size > t.MaxSize {
if t.size > t.MaxSize || len(t.blocks) > t.MaxBlocks {
return nd.Cid(), t.Commit()
}
return nd.Cid(), nil
Expand Down

0 comments on commit 88af4b7

Please sign in to comment.