feat(v0.4): add WithLock helper that warns-and-skips on timeout
This commit is contained in:
@@ -52,3 +52,20 @@ func Acquire(lockPath string, timeout, staleAfter time.Duration) (func(), error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithLock acquires the lock at lockPath, runs fn while holding it, and
|
||||
// releases the lock. If the lock cannot be acquired within timeout, fn is
|
||||
// NOT called and (skipped=true, err=nil) is returned — this matches the
|
||||
// ctask "warn and skip, never hang" contract for metadata writes.
|
||||
// Any error returned by fn is surfaced to the caller with skipped=false.
|
||||
func WithLock(lockPath string, timeout, staleAfter time.Duration, fn func() error) (skipped bool, err error) {
|
||||
release, err := Acquire(lockPath, timeout, staleAfter)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrTimeout) {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
defer release()
|
||||
return false, fn()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user