Skip to content

Commit

Permalink
add race fix for HashOnRead
Browse files Browse the repository at this point in the history
This commit was moved from ipfs/go-ipfs-blockstore@2bd301c
  • Loading branch information
postables committed Apr 12, 2020
1 parent cf01195 commit 960beb2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
dsq "github.com/ipfs/go-datastore/query"
dshelp "github.com/ipfs/go-ipfs-ds-help"
logging "github.com/ipfs/go-log"
uatomic "go.uber.org/atomic"
)

var log = logging.Logger("blockstore")
Expand Down Expand Up @@ -101,17 +102,18 @@ func NewBlockstore(d ds.Batching) Blockstore {
dsb = dd
return &blockstore{
datastore: dsb,
rehash: uatomic.NewBool(false),
}
}

type blockstore struct {
datastore ds.Batching

rehash bool
rehash *uatomic.Bool
}

func (bs *blockstore) HashOnRead(enabled bool) {
bs.rehash = enabled
bs.rehash.Store(enabled)
}

func (bs *blockstore) Get(k cid.Cid) (blocks.Block, error) {
Expand All @@ -126,7 +128,7 @@ func (bs *blockstore) Get(k cid.Cid) (blocks.Block, error) {
if err != nil {
return nil, err
}
if bs.rehash {
if bs.rehash.Load() {
rbcid, err := k.Prefix().Sum(bdata)
if err != nil {
return nil, err
Expand Down

0 comments on commit 960beb2

Please sign in to comment.