Commit ebae4323 authored by Jeromy's avatar Jeromy

merkledag: limit number of objects in a batch to prevent out of fd issues

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent ec392edf
...@@ -68,7 +68,15 @@ func (n *dagService) Add(nd node.Node) (*cid.Cid, error) { ...@@ -68,7 +68,15 @@ func (n *dagService) Add(nd node.Node) (*cid.Cid, error) {
} }
func (n *dagService) Batch() *Batch { 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 // Get retrieves a node from the dagService, fetching the block in the BlockService
...@@ -379,12 +387,13 @@ type Batch struct { ...@@ -379,12 +387,13 @@ type Batch struct {
blocks []blocks.Block blocks []blocks.Block
size int size int
MaxSize int MaxSize int
MaxBlocks int
} }
func (t *Batch) Add(nd node.Node) (*cid.Cid, error) { func (t *Batch) Add(nd node.Node) (*cid.Cid, error) {
t.blocks = append(t.blocks, nd) t.blocks = append(t.blocks, nd)
t.size += len(nd.RawData()) 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(), t.Commit()
} }
return nd.Cid(), nil return nd.Cid(), nil
......
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