Standardize daemon detection with tryDaemonLock probe (bd-wgu4)
- Extract lock checking to internal/lockfile package - Add lock probe in RPC client before connection attempts - Update daemon discovery to use lock probe - Eliminates unnecessary connection attempts when socket missing Closes bd-wgu4 Amp-Thread-ID: https://ampcode.com/threads/T-3b863f21-3af4-49d3-9214-477d904b80fe Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
21
internal/lockfile/lock_unix.go
Normal file
21
internal/lockfile/lock_unix.go
Normal file
@@ -0,0 +1,21 @@
|
||||
//go:build unix
|
||||
|
||||
package lockfile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var errDaemonLocked = errors.New("daemon lock already held by another process")
|
||||
|
||||
// flockExclusive acquires an exclusive non-blocking lock on the file
|
||||
func flockExclusive(f *os.File) error {
|
||||
err := unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB)
|
||||
if err == unix.EWOULDBLOCK {
|
||||
return errDaemonLocked
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user