blocks/blockstorage: use automic for bloom.active
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Jul 4, 2016 at 20:12 UTC
98f877af81584b7062fd28b67095550ba298bf37
1 file changed
+7
-5
blocks/blockstore/bloom_cache.go
+7
-5
@@ -7,6 +7,8 @@ import (
7
bloom "gx/ipfs/QmWQ2SJisXwcCLsUXLwYCKSfyExXjFRW2WbBH5sqCUnwX5/bbloom"
8
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
9
ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore"
10
+
11
+ "sync/atomic"
12
)
13
14
// BloomCached returns Blockstore that caches Has requests using Bloom filter
@@ -29,7 +31,7 @@ func BloomCached(bs Blockstore, bloomSize, lruSize int) (*bloomcache, error) {
31
32
type bloomcache struct {
33
bloom *bloom.Bloom
32
- active bool
34
+ active int32
35
36
arc *lru.ARCCache
37
// This chan is only used for testing to wait for bloom to enable
@@ -43,11 +45,11 @@ type bloomcache struct {
45
46
func (b *bloomcache) Invalidate() {
47
b.rebuildChan = make(chan struct{})
46
- b.active = false
48
+ atomic.StoreInt32(&b.active, 0)
49
}
50
51
func (b *bloomcache) BloomActive() bool {
50
- return b.active
52
+ return atomic.LoadInt32(&b.active) != 0
53
}
54
55
func (b *bloomcache) Rebuild() {
@@ -64,7 +66,7 @@ func (b *bloomcache) Rebuild() {
66
b.bloom.AddTS([]byte(key)) // Use binary key, the more compact the better
67
}
68
close(b.rebuildChan)
67
- b.active = true
69
+ atomic.StoreInt32(&b.active, 1)
70
}
71
72
func (b *bloomcache) DeleteBlock(k key.Key) error {
@@ -89,7 +91,7 @@ func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) {
91
if k == "" {
92
return true, true
93
}
92
- if b.active {
94
+ if b.BloomActive() {
95
blr := b.bloom.HasTS([]byte(k))
96
if blr == false { // not contained in bloom is only conclusive answer bloom gives
97
return blr, true