inital commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package mutex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const maxMutexLength = 64
|
||||
|
||||
func sanitizeName(name string) (string, error) {
|
||||
trimmed := strings.TrimSpace(name)
|
||||
if trimmed == "" {
|
||||
return "", fmt.Errorf("empty mutex name")
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
for _, r := range trimmed {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-' || r == '_' || r == '.' {
|
||||
builder.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
sanitized := builder.String()
|
||||
if sanitized == "" {
|
||||
return "", fmt.Errorf("mutex name has no valid characters")
|
||||
}
|
||||
|
||||
if len(sanitized) > maxMutexLength {
|
||||
sanitized = sanitized[:maxMutexLength]
|
||||
}
|
||||
|
||||
return sanitized, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//go:build !windows
|
||||
|
||||
package mutex
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func Acquire(name string) (func(), bool, error) {
|
||||
if name == "" {
|
||||
return func() {}, true, nil
|
||||
}
|
||||
|
||||
sanitized, err := sanitizeName(name)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
lockPath := filepath.Join(os.TempDir(), fmt.Sprintf("overlord-%s.lock", sanitized))
|
||||
file, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o644)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("open mutex file: %w", err)
|
||||
}
|
||||
|
||||
if err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
_ = file.Close()
|
||||
if errors.Is(err, unix.EWOULDBLOCK) {
|
||||
return func() {}, false, nil
|
||||
}
|
||||
return nil, false, fmt.Errorf("lock mutex file: %w", err)
|
||||
}
|
||||
|
||||
release := func() {
|
||||
_ = unix.Flock(int(file.Fd()), unix.LOCK_UN)
|
||||
_ = file.Close()
|
||||
}
|
||||
|
||||
return release, true, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//go:build windows
|
||||
|
||||
package mutex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func Acquire(name string) (func(), bool, error) {
|
||||
if name == "" {
|
||||
return func() {}, true, nil
|
||||
}
|
||||
|
||||
sanitized, err := sanitizeName(name)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
fullName := "Global\\Overlord-" + sanitized
|
||||
handle, err := windows.CreateMutex(nil, false, windows.StringToUTF16Ptr(fullName))
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("create mutex: %w", err)
|
||||
}
|
||||
|
||||
if windows.GetLastError() == windows.ERROR_ALREADY_EXISTS {
|
||||
windows.CloseHandle(handle)
|
||||
return func() {}, false, nil
|
||||
}
|
||||
|
||||
release := func() {
|
||||
_ = windows.ReleaseMutex(handle)
|
||||
_ = windows.CloseHandle(handle)
|
||||
}
|
||||
|
||||
return release, true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user