blob: bc3ae8a3b64ce7ada2cd3c96b83bef348de1b9cc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// Package filelock provides advisory exclusive locks on open files.
package filelock
import (
"context"
"errors"
"os"
"time"
)
// ErrWouldBlock indicates a non-blocking lock attempt could not acquire the lock.
var ErrWouldBlock = errors.New("filelock: would block")
// TryExclusive attempts a non-blocking exclusive advisory lock on f.
func TryExclusive(f *os.File) error {
return tryLockExclusive(f.Fd())
}
// UnlockExclusive releases the advisory lock held on f.
func UnlockExclusive(f *os.File) error {
return unlockExclusive(f.Fd())
}
// retryInterval is the backoff between successive non-blocking lock attempts.
const retryInterval = 5 * time.Millisecond
// AcquireExclusive spins with TryExclusive until the lock is acquired, ctx is done, or a non-would-block error occurs.
func AcquireExclusive(ctx context.Context, f *os.File) (func() error, error) {
fd := f.Fd()
for {
err := tryLockExclusive(fd)
if err == nil {
return func() error { return unlockExclusive(fd) }, nil
}
if errors.Is(err, ErrWouldBlock) {
// Use a fresh timer per iteration via time.After instead of reusing and
// Reset()-ing a single timer. Resetting a timer that may still be pending is
// the documented hazard in the Go timer API: a stale value can already be
// queued on the channel, causing a spurious early wake-up. A new timer each
// loop guarantees a clean, full retryInterval delay (or ctx cancellation).
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(retryInterval):
}
continue
}
return nil, err
}
}
|