Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-blockstore#50 from RTradeLtd/racef
Browse files Browse the repository at this point in the history
add race fix for HashOnRead

This commit was moved from ipfs/go-ipfs-blockstore@8549146
  • Loading branch information
Stebalien authored Apr 13, 2020
2 parents cf01195 + 960beb2 commit 7599211
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 7599211

Please sign in to comment.