feat: add durable client command spool

This commit is contained in:
2026-09-06 06:28:42 +00:00
parent 3e675aaabf
commit c348d6bb16
8 changed files with 986 additions and 1 deletions
+47
View File
@@ -0,0 +1,47 @@
//go:build !windows
package spool
import (
"errors"
"os"
"syscall"
)
func acquireInstanceLock(path string) (func() error, error) {
if err := ensurePrivateFile(path); err != nil {
return nil, err
}
file, err := os.OpenFile(path, os.O_RDWR, 0)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
_ = file.Close()
if errors.Is(err, syscall.EWOULDBLOCK) {
return nil, ErrAlreadyOpen
}
return nil, err
}
return func() error {
unlockErr := syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
closeErr := file.Close()
if unlockErr != nil {
return unlockErr
}
return closeErr
}, nil
}
func syncDirectory(path string) error {
directory, err := os.Open(path)
if err != nil {
return err
}
defer directory.Close()
return directory.Sync()
}
func replaceIdentityFile(temporaryPath, targetPath string) error {
return os.Rename(temporaryPath, targetPath)
}