Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed mutex in cache repository to read write mutex so that all concurrent reads are allowed #182

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading