From bba71da401961b5982809e95d6aef233c9f277d1 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 11 Mar 2021 20:30:43 -0800 Subject: [PATCH] fix: return buffers after canceling badger operation In theory, Delete/Put could fail. If it does, we'll return the buffers to the pool before we're really done with them. In practice, this is almost certainly not an issue as badger shouldn't _use_ the buffer unless we flush. But I feel slightly safer this way. --- blockstore/badger/blockstore.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 2c00f424077..ff3781ca4d1 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -288,9 +288,6 @@ func (b *Blockstore) PutMany(blocks []blocks.Block) error { return ErrBlockstoreClosed } - batch := b.DB.NewWriteBatch() - defer batch.Cancel() - // toReturn tracks the byte slices to return to the pool, if we're using key // prefixing. we can't return each slice to the pool after each Set, because // badger holds on to the slice. @@ -304,6 +301,9 @@ func (b *Blockstore) PutMany(blocks []blocks.Block) error { }() } + batch := b.DB.NewWriteBatch() + defer batch.Cancel() + for _, block := range blocks { k, pooled := b.PooledStorageKey(block.Cid()) if pooled { @@ -342,9 +342,6 @@ func (b *Blockstore) DeleteMany(cids []cid.Cid) error { return ErrBlockstoreClosed } - batch := b.DB.NewWriteBatch() - defer batch.Cancel() - // toReturn tracks the byte slices to return to the pool, if we're using key // prefixing. we can't return each slice to the pool after each Set, because // badger holds on to the slice. @@ -358,6 +355,9 @@ func (b *Blockstore) DeleteMany(cids []cid.Cid) error { }() } + batch := b.DB.NewWriteBatch() + defer batch.Cancel() + for _, cid := range cids { k, pooled := b.PooledStorageKey(cid) if pooled {