image: Add lock around image cache access

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
Mrunal Patel 2018-02-14 17:55:31 -08:00 committed by Nalin Dahyabhai
parent c53211eacd
commit 125ec8a7bd

View file

@ -5,6 +5,7 @@ import (
"net" "net"
"path" "path"
"strings" "strings"
"sync"
"github.com/containers/image/copy" "github.com/containers/image/copy"
"github.com/containers/image/docker/reference" "github.com/containers/image/docker/reference"
@ -63,6 +64,7 @@ type imageService struct {
indexConfigs map[string]*indexInfo indexConfigs map[string]*indexInfo
registries []string registries []string
imageCache map[string]imageCacheItem imageCache map[string]imageCacheItem
imageCacheLock sync.Mutex
} }
// sizer knows its size. // sizer knows its size.
@ -249,16 +251,21 @@ func (svc *imageService) ListImages(systemContext *types.SystemContext, filter s
} }
} }
// Handle the removals. // Handle the removals.
svc.imageCacheLock.Lock()
for _, removedID := range removedIDs { for _, removedID := range removedIDs {
delete(svc.imageCache, removedID) delete(svc.imageCache, removedID)
} }
svc.imageCacheLock.Unlock()
}() }()
for _, image := range images { for _, image := range images {
visited[image.ID] = struct{}{} visited[image.ID] = struct{}{}
var user string var user string
var size *uint64 var size *uint64
var configDigest digest.Digest var configDigest digest.Digest
if cacheItem, ok := svc.imageCache[image.ID]; ok { svc.imageCacheLock.Lock()
cacheItem, ok := svc.imageCache[image.ID]
svc.imageCacheLock.Unlock()
if ok {
user, size, configDigest = cacheItem.user, cacheItem.size, cacheItem.configDigest user, size, configDigest = cacheItem.user, cacheItem.size, cacheItem.configDigest
} else { } else {
ref, err := istorage.Transport.ParseStoreReference(svc.store, "@"+image.ID) ref, err := istorage.Transport.ParseStoreReference(svc.store, "@"+image.ID)
@ -291,7 +298,9 @@ func (svc *imageService) ListImages(systemContext *types.SystemContext, filter s
size: size, size: size,
configDigest: configDigest, configDigest: configDigest,
} }
svc.imageCacheLock.Lock()
svc.imageCache[image.ID] = cacheItem svc.imageCache[image.ID] = cacheItem
svc.imageCacheLock.Unlock()
} }
name, tags, digests := sortNamesByType(image.Names) name, tags, digests := sortNamesByType(image.Names)
imageDigest, repoDigests := svc.makeRepoDigests(digests, tags, image.ID) imageDigest, repoDigests := svc.makeRepoDigests(digests, tags, image.ID)