GetLock returns a lock for the specified operation type
(lockType LockType)
| 50 | |
| 51 | // GetLock returns a lock for the specified operation type |
| 52 | func (rlm *RepositoryLockManager) GetLock(lockType LockType) *RepositoryLock { |
| 53 | rlm.mu.Lock() |
| 54 | defer rlm.mu.Unlock() |
| 55 | |
| 56 | if lock, exists := rlm.locks[lockType]; exists { |
| 57 | return lock |
| 58 | } |
| 59 | |
| 60 | lockFileName := fmt.Sprintf("container-use-%x-%s.lock", hashString(rlm.repoPath), string(lockType)) |
| 61 | lockDir := filepath.Join(os.TempDir(), "container-use-locks") |
| 62 | lockFile := filepath.Join(lockDir, lockFileName) |
| 63 | |
| 64 | err := os.MkdirAll(lockDir, 0755) |
| 65 | if err != nil { |
| 66 | slog.Error("Failed to create lock directory", "error", err) |
| 67 | } |
| 68 | |
| 69 | lock := &RepositoryLock{ |
| 70 | flock: flock.New(lockFile), |
| 71 | } |
| 72 | |
| 73 | rlm.locks[lockType] = lock |
| 74 | return lock |
| 75 | } |
| 76 | |
| 77 | // WithLock executes a function while holding an exclusive lock for the specified lock type |
| 78 | func (rlm *RepositoryLockManager) WithLock(ctx context.Context, lockType LockType, fn func() error) error { |
no test coverage detected