Skip to content

Commit

Permalink
Merge pull request #1500 from 0chain/fix/cache-lock
Browse files Browse the repository at this point in the history
Fix cache lock
  • Loading branch information
dabasov authored Nov 2, 2024
2 parents edddb25 + a92fb1e commit c8b0659
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions code/go/0chain.net/blobbercore/reference/dbCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type RefCache struct {
}

var (
cacheMap = make(map[string]*RefCache)
cacheMap = make(map[string]*RefCache)
cacheMapLock sync.Mutex
)

func NewCollector(changes int) QueryCollector {
Expand Down Expand Up @@ -91,9 +92,11 @@ func (dc *dbCollector) Finalize(ctx context.Context, allocationID, allocationRoo
}
}
if allocationID != "" {
cacheMapLock.Lock()
dc.refCache.AllocationRoot = allocationRoot
cacheMap[allocationID] = &(dc.refCache)
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))
cacheMapLock.Unlock()
}
return nil
}
Expand All @@ -111,11 +114,15 @@ func (dc *dbCollector) GetFromCache(lookupHash string) *Ref {
}

func GetRefCache(allocationID string) *RefCache {
cacheMapLock.Lock()
defer cacheMapLock.Unlock()
return cacheMap[allocationID]
}

func DeleteRefCache(allocationID string) {
cacheMap[allocationID] = nil
cacheMapLock.Lock()
delete(cacheMap, allocationID)
cacheMapLock.Unlock()
}

func (dc *dbCollector) LockTransaction() {
Expand Down

0 comments on commit c8b0659

Please sign in to comment.