Files
dibs/internal/fsutil/lock.go
T
Overlord 2175c0e5aa Add scheduler, rate limiting and headless mode.
Hard-coded 60s URL and 10s host floors with jitter and per-host
backoff, shared between dibs processes through a locked state file.
The scheduler shuffles due URLs each round and runs one worker per
host; 'dibs --headless' watches and logs JSON.
2026-10-14 09:04:15 +02:00

27 lines
583 B
Go

package fsutil
import (
"os"
"path/filepath"
)
// Lock takes an exclusive lock on path, creating it if needed, and blocks
// until it gets it. The lock is shared with other processes. The returned
// function releases it.
func Lock(path string) (func(), error) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return nil, err }
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil { return nil, err }
if err := lockFile(f); err != nil {
_ = f.Close()
return nil, err
}
return func() {
_ = unlockFile(f)
_ = f.Close()
}, nil
}