Skip to content

Commit c8b0659

Browse files
authored
Merge pull request #1500 from 0chain/fix/cache-lock
Fix cache lock
2 parents edddb25 + a92fb1e commit c8b0659

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

code/go/0chain.net/blobbercore/reference/dbCollector.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type RefCache struct {
3636
}
3737

3838
var (
39-
cacheMap = make(map[string]*RefCache)
39+
cacheMap = make(map[string]*RefCache)
40+
cacheMapLock sync.Mutex
4041
)
4142

4243
func NewCollector(changes int) QueryCollector {
@@ -91,9 +92,11 @@ func (dc *dbCollector) Finalize(ctx context.Context, allocationID, allocationRoo
9192
}
9293
}
9394
if allocationID != "" {
95+
cacheMapLock.Lock()
9496
dc.refCache.AllocationRoot = allocationRoot
9597
cacheMap[allocationID] = &(dc.refCache)
9698
logging.Logger.Info("Finalize", zap.Int("created", len(dc.createdRefs)), zap.Int("deleted", len(dc.deletedRefs)), zap.String("allocation_root", cacheMap[allocationID].AllocationRoot), zap.String("allocation_id", allocationID))
99+
cacheMapLock.Unlock()
97100
}
98101
return nil
99102
}
@@ -111,11 +114,15 @@ func (dc *dbCollector) GetFromCache(lookupHash string) *Ref {
111114
}
112115

113116
func GetRefCache(allocationID string) *RefCache {
117+
cacheMapLock.Lock()
118+
defer cacheMapLock.Unlock()
114119
return cacheMap[allocationID]
115120
}
116121

117122
func DeleteRefCache(allocationID string) {
118-
cacheMap[allocationID] = nil
123+
cacheMapLock.Lock()
124+
delete(cacheMap, allocationID)
125+
cacheMapLock.Unlock()
119126
}
120127

121128
func (dc *dbCollector) LockTransaction() {

0 commit comments

Comments
 (0)