image: Add lock around image cache access
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
parent
c53211eacd
commit
125ec8a7bd
1 changed files with 10 additions and 1 deletions
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue