1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

layers: remove layerStore.getWithoutLock()

This function was abstracting things a bit too much; the layerStore had a
exported `.Get()` which called `.getWithoutLock()`, but also a non-exported
`.get()`, which also called `.getWithoutLock()`.

While it's common to have a non-exported variant (without locking), the naming
of `.get()` could easily be confused for that variant (which it wasn't).

All locations where `.get()` was called were already handling locks for
`releaseLayer()`, so moving the actual locking inline for `.get()` makes it
more visible where locking happens.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-28 19:57:37 +01:00
parent d809ad98e3
commit 203fcd6997
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 10 additions and 12 deletions

View file

@ -284,7 +284,9 @@ func (ls *layerStore) registerWithDescriptor(ts io.Reader, parent ChainID, descr
var p *roLayer
if string(parent) != "" {
ls.layerL.Lock()
p = ls.get(parent)
ls.layerL.Unlock()
if p == nil {
return nil, ErrLayerDoesNotExist
}
@ -351,7 +353,7 @@ func (ls *layerStore) registerWithDescriptor(ts io.Reader, parent ChainID, descr
ls.layerL.Lock()
defer ls.layerL.Unlock()
if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
if existingLayer := ls.get(layer.chainID); existingLayer != nil {
// Set error for cleanup, but do not return the error
err = errors.New("layer already exists")
return existingLayer.getReference(), nil
@ -366,28 +368,20 @@ func (ls *layerStore) registerWithDescriptor(ts io.Reader, parent ChainID, descr
return layer.getReference(), nil
}
func (ls *layerStore) getWithoutLock(layer ChainID) *roLayer {
func (ls *layerStore) get(layer ChainID) *roLayer {
l, ok := ls.layerMap[layer]
if !ok {
return nil
}
l.referenceCount++
return l
}
func (ls *layerStore) get(l ChainID) *roLayer {
ls.layerL.Lock()
defer ls.layerL.Unlock()
return ls.getWithoutLock(l)
}
func (ls *layerStore) Get(l ChainID) (Layer, error) {
ls.layerL.Lock()
defer ls.layerL.Unlock()
layer := ls.getWithoutLock(l)
layer := ls.get(l)
if layer == nil {
return nil, ErrLayerDoesNotExist
}
@ -520,7 +514,9 @@ func (ls *layerStore) CreateRWLayer(name string, parent ChainID, opts *CreateRWL
var pid string
var p *roLayer
if string(parent) != "" {
ls.layerL.Lock()
p = ls.get(parent)
ls.layerL.Unlock()
if p == nil {
return nil, ErrLayerDoesNotExist
}

View file

@ -87,7 +87,9 @@ func (ls *layerStore) RegisterByGraphID(graphID string, parent ChainID, diffID D
var err error
var p *roLayer
if string(parent) != "" {
ls.layerL.Lock()
p = ls.get(parent)
ls.layerL.Unlock()
if p == nil {
return nil, ErrLayerDoesNotExist
}
@ -117,7 +119,7 @@ func (ls *layerStore) RegisterByGraphID(graphID string, parent ChainID, diffID D
ls.layerL.Lock()
defer ls.layerL.Unlock()
if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
if existingLayer := ls.get(layer.chainID); existingLayer != nil {
// Set error for cleanup, but do not return
err = errors.New("layer already exists")
return existingLayer.getReference(), nil