From a888ea0d1fa47d9ec4c6bb0fc3abcdd1c54bab70 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 11 Mar 2021 20:03:38 -0800 Subject: [PATCH] fix: avoid holding a lock while calling the View callback Interleaved puts/views could get really slow and there's no real reason to use view under the covers here because the underlying blockstore is always a "memstore". --- blockstore/timed.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/blockstore/timed.go b/blockstore/timed.go index ce25bb5bc..80e6c8a08 100644 --- a/blockstore/timed.go +++ b/blockstore/timed.go @@ -103,13 +103,20 @@ func (t *TimedCacheBlockstore) PutMany(bs []blocks.Block) error { } func (t *TimedCacheBlockstore) View(k cid.Cid, callback func([]byte) error) error { + // The underlying blockstore is always a "mem" blockstore so there's no difference, + // from a performance perspective, between view & get. So we call Get to avoid + // calling an arbitrary callback while holding a lock. t.mu.RLock() - defer t.mu.RUnlock() - err := t.active.View(k, callback) + block, err := t.active.Get(k) if err == ErrNotFound { - err = t.inactive.View(k, callback) + block, err = t.inactive.Get(k) } - return err + t.mu.RUnlock() + + if err != nil { + return err + } + return callback(block.RawData()) } func (t *TimedCacheBlockstore) Get(k cid.Cid) (blocks.Block, error) {