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".
This commit is contained in:
parent
d0243904f7
commit
a888ea0d1f
@ -103,13 +103,20 @@ func (t *TimedCacheBlockstore) PutMany(bs []blocks.Block) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TimedCacheBlockstore) View(k cid.Cid, callback func([]byte) error) 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()
|
t.mu.RLock()
|
||||||
defer t.mu.RUnlock()
|
block, err := t.active.Get(k)
|
||||||
err := t.active.View(k, callback)
|
|
||||||
if err == ErrNotFound {
|
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) {
|
func (t *TimedCacheBlockstore) Get(k cid.Cid) (blocks.Block, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user