Scope writer locks to each writer.

Signed-off-by: Niklas Gehlen <niklas@namespacelabs.com>
This commit is contained in:
Niklas Gehlen
2024-10-23 20:39:51 +02:00
parent b291eb802b
commit 2535b187a6
4 changed files with 23 additions and 24 deletions

View File

@@ -18,7 +18,6 @@ package local
import (
"fmt"
"sync"
"time"
"github.com/containerd/errdefs"
@@ -30,17 +29,11 @@ type lock struct {
since time.Time
}
var (
// locks lets us lock in process
locks = make(map[string]*lock)
locksMu sync.Mutex
)
func (s *store) tryLock(ref string) error {
s.locksMu.Lock()
defer s.locksMu.Unlock()
func tryLock(ref string) error {
locksMu.Lock()
defer locksMu.Unlock()
if v, ok := locks[ref]; ok {
if v, ok := s.locks[ref]; ok {
// Returning the duration may help developers distinguish dead locks (long duration) from
// lock contentions (short duration).
now := time.Now()
@@ -50,13 +43,13 @@ func tryLock(ref string) error {
)
}
locks[ref] = &lock{time.Now()}
s.locks[ref] = &lock{time.Now()}
return nil
}
func unlock(ref string) {
locksMu.Lock()
defer locksMu.Unlock()
func (s *store) unlock(ref string) {
s.locksMu.Lock()
defer s.locksMu.Unlock()
delete(locks, ref)
delete(s.locks, ref)
}