生产部署整改阶段检查点
This commit is contained in:
1 parent
f3348cf95d
commit
92d4b3d398
56 files changed
+4178
-240
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var ErrLocked = errors.New("进程锁已被占用")
|
||||
|
||||
type Lock struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func Acquire(path string) (*Lock, error) {
|
||||
path = filepath.Clean(path)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return nil, fmt.Errorf("创建进程锁目录失败:%w", err)
|
||||
}
|
||||
if info, err := os.Lstat(path); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return nil, errors.New("进程锁路径必须是普通文件")
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("检查进程锁失败:%w", err)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开进程锁失败:%w", err)
|
||||
}
|
||||
if err := lockFile(file); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chmod(path, 0o600); err != nil {
|
||||
_ = unlockFile(file)
|
||||
_ = file.Close()
|
||||
return nil, fmt.Errorf("设置进程锁权限失败:%w", err)
|
||||
}
|
||||
return &Lock{file: file}, nil
|
||||
}
|
||||
|
||||
func (lock *Lock) Close() error {
|
||||
if lock == nil || lock.file == nil {
|
||||
return nil
|
||||
}
|
||||
file := lock.file
|
||||
lock.file = nil
|
||||
return errors.Join(unlockFile(file), file.Close())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAcquirePreventsConcurrentProcessAccess(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "scert.lock")
|
||||
first, err := Acquire(path)
|
||||
if err != nil {
|
||||
t.Fatalf("获取第一个进程锁失败:%v", err)
|
||||
}
|
||||
defer first.Close()
|
||||
|
||||
if _, err := Acquire(path); !errors.Is(err, ErrLocked) {
|
||||
t.Fatalf("并发进程锁未被拒绝:%v", err)
|
||||
}
|
||||
if err := first.Close(); err != nil {
|
||||
t.Fatalf("释放第一个进程锁失败:%v", err)
|
||||
}
|
||||
second, err := Acquire(path)
|
||||
if err != nil {
|
||||
t.Fatalf("释放后无法重新获取进程锁:%v", err)
|
||||
}
|
||||
if err := second.Close(); err != nil {
|
||||
t.Fatalf("释放第二个进程锁失败:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//go:build !windows
|
||||
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func lockFile(file *os.File) error {
|
||||
err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB)
|
||||
if errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, unix.EAGAIN) {
|
||||
return ErrLocked
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unlockFile(file *os.File) error {
|
||||
if err := unix.Flock(int(file.Fd()), unix.LOCK_UN); err != nil {
|
||||
return fmt.Errorf("释放进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//go:build windows
|
||||
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func lockFile(file *os.File) error {
|
||||
overlapped := new(windows.Overlapped)
|
||||
err := windows.LockFileEx(
|
||||
windows.Handle(file.Fd()),
|
||||
windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
overlapped,
|
||||
)
|
||||
if errors.Is(err, windows.ERROR_LOCK_VIOLATION) {
|
||||
return ErrLocked
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unlockFile(file *os.File) error {
|
||||
overlapped := new(windows.Overlapped)
|
||||
if err := windows.UnlockFileEx(windows.Handle(file.Fd()), 0, 1, 0, overlapped); err != nil {
|
||||
return fmt.Errorf("释放进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user