Skip to content

Commit

Permalink
changed mutex in cache repository to read write mutex so that all con…
Browse files Browse the repository at this point in the history
…current reads are allowed
  • Loading branch information
sriharshabm committed Feb 14, 2025
1 parent 1d6ba30 commit e174980
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/cache/memory/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type cachedRepository struct {

lastVersion string

mutex sync.Mutex
mutex sync.RWMutex
cachedPackageRevisions map[repository.PackageRevisionKey]*cachedPackageRevision
cachedPackages map[repository.PackageKey]*cachedPackage
// Error encountered on repository refresh by the refresh goroutine.
Expand Down Expand Up @@ -108,8 +108,8 @@ func (r *cachedRepository) ListPackageRevisions(ctx context.Context, filter repo
}

func (r *cachedRepository) getRefreshError() error {
r.mutex.Lock()
defer r.mutex.Unlock()
r.mutex.RLock()
defer r.mutex.RUnlock()

// TODO: This should also check r.refreshPkgsError when
// the package resource is fully supported.
Expand All @@ -123,8 +123,8 @@ func (r *cachedRepository) getPackageRevisions(ctx context.Context, filter repos
if err != nil {
return nil, err
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.mutex.RLock()
defer r.mutex.RUnlock()
return toPackageRevisionSlice(ctx, packageRevisions, filter), nil
}

Expand All @@ -134,16 +134,16 @@ func (r *cachedRepository) getPackages(ctx context.Context, filter repository.Li
if err != nil {
return nil, err
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.mutex.RLock()
defer r.mutex.RUnlock()
return toPackageSlice(packages, filter), nil
}

// getCachedPackages returns cachedPackages; fetching it if not cached or if forceRefresh.
// mutex must be held.
func (r *cachedRepository) getCachedPackages(ctx context.Context, forceRefresh bool) (map[repository.PackageKey]*cachedPackage, map[repository.PackageRevisionKey]*cachedPackageRevision, error) {
// must hold mutex

r.mutex.Lock()
packages := r.cachedPackages
packageRevisions := r.cachedPackageRevisions
Expand All @@ -157,6 +157,7 @@ func (r *cachedRepository) getCachedPackages(ctx context.Context, forceRefresh b
// TODO: Figure out a way to do this without the cache layer
// needing to know what type of repo we are working with.
if err := gitRepo.UpdateDeletionProposedCache(); err != nil {
r.mutex.Unlock()
return nil, nil, err
}
}
Expand Down

0 comments on commit e174980

Please sign in to comment.