-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): enable parallel writes by using per-repo locking
Signed-off-by: Andrei Aaron <[email protected]>
- Loading branch information
Showing
9 changed files
with
192 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package imagestore | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type ImageStoreLock struct { | ||
// locks per repository paths | ||
repoLocks sync.Map | ||
// lock for the entire storage, needed in case all repos need to be processed | ||
// including blocking creating new repos | ||
globalLock *sync.RWMutex | ||
} | ||
|
||
func NewImageStoreLock() *ImageStoreLock { | ||
return &ImageStoreLock{ | ||
repoLocks: sync.Map{}, | ||
globalLock: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (sl *ImageStoreLock) RLock() { | ||
// block reads and writes to the entire storage, including new repos | ||
sl.globalLock.RLock() | ||
} | ||
|
||
func (sl *ImageStoreLock) RUnlock() { | ||
// unlock to the storage in general | ||
sl.globalLock.RUnlock() | ||
} | ||
|
||
func (sl *ImageStoreLock) Lock() { | ||
// block reads and writes to the entire storage, including new repos | ||
sl.globalLock.Lock() | ||
} | ||
|
||
func (sl *ImageStoreLock) Unlock() { | ||
// unlock to the storage in general | ||
sl.globalLock.Unlock() | ||
} | ||
|
||
func (sl *ImageStoreLock) RLockRepo(repo string) { | ||
// besides the individual repo increment the read counter for the | ||
// global lock, this will make sure the storage cannot be | ||
// write-locked at global level while individual repos are accessed | ||
sl.globalLock.RLock() | ||
|
||
val, _ := sl.repoLocks.LoadOrStore(repo, &sync.RWMutex{}) | ||
|
||
// lock individual repo | ||
repoLock := val.(*sync.RWMutex) | ||
repoLock.RLock() | ||
} | ||
|
||
func (sl *ImageStoreLock) RUnlockRepo(repo string) { | ||
// decrement the global read counter | ||
sl.globalLock.RUnlock() | ||
|
||
val, ok := sl.repoLocks.Load(repo) | ||
if !ok { | ||
// somehow the unlock is called for repo that was never locked | ||
return | ||
} | ||
|
||
// read-unlock individual repo | ||
repoLock := val.(*sync.RWMutex) | ||
repoLock.RUnlock() | ||
} | ||
|
||
func (sl *ImageStoreLock) LockRepo(repo string) { | ||
// besides the individual repo increment the read counter for the | ||
// global lock, this will make sure the storage cannot be | ||
// write-locked at global level while individual repos are accessed | ||
// we are not using the write lock here, as that would make all repos | ||
// wait for one another | ||
sl.globalLock.RLock() | ||
|
||
val, _ := sl.repoLocks.LoadOrStore(repo, &sync.RWMutex{}) | ||
|
||
// write-lock individual repo | ||
repoLock := val.(*sync.RWMutex) | ||
repoLock.Lock() | ||
} | ||
|
||
func (sl *ImageStoreLock) UnlockRepo(repo string) { | ||
// decrement the global read counter | ||
sl.globalLock.RUnlock() | ||
|
||
val, ok := sl.repoLocks.Load(repo) | ||
if !ok { | ||
// somehow the unlock is called for a repo that was never locked | ||
return | ||
} | ||
|
||
// write-unlock individual repo | ||
repoLock := val.(*sync.RWMutex) | ||
repoLock.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.