diff --git a/primitives/bloom.go b/primitives/bloom.go index 6e0b204..877180b 100644 --- a/primitives/bloom.go +++ b/primitives/bloom.go @@ -2,11 +2,13 @@ package primitives import ( "crypto/sha256" + "sync" ) // BloomFilter implements a bloom filter type BloomFilter struct { B []bool + lock sync.Mutex } // Init constructs a bloom filter of size m @@ -38,9 +40,11 @@ func (bf BloomFilter) Hash(msg []byte) []int { return []int{pos1, pos2, pos3, pos4} } -// Insert updates the BloomFilter +// Insert updates the BloomFilter (suitable for concurrent use) func (bf *BloomFilter) Insert(msg []byte) { pos := bf.Hash(msg) + bf.lock.Lock() + defer bf.lock.Unlock() bf.B[pos[0]] = true bf.B[pos[1]] = true bf.B[pos[2]] = true