mmm: lockfile to prevent multiple instances.

This commit is contained in:
fiatjaf
2025-10-28 19:09:28 -03:00
parent b3c617e36b
commit 0aa0038c40
2 changed files with 48 additions and 0 deletions

View File

@@ -56,6 +56,15 @@ func (b *MultiMmapManager) Init() error {
b.Logger = &nopLogger
}
// create lockfile to prevent multiple instances
lockfilePath := filepath.Join(b.Dir, "mmmm.lock")
if _, err := os.OpenFile(lockfilePath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644); err != nil {
if os.IsExist(err) {
return fmt.Errorf("database at %s is already in use by another instance", b.Dir)
}
return fmt.Errorf("failed to create lockfile %s: %w", lockfilePath, err)
}
// create directory if it doesn't exist
dbpath := filepath.Join(b.Dir, "mmmm")
if err := os.MkdirAll(dbpath, 0755); err != nil {
@@ -333,4 +342,8 @@ func (b *MultiMmapManager) Close() {
}
syscall.Munmap(b.mmapf)
// remove lockfile
lockfilePath := filepath.Join(b.Dir, "mmmm.lock")
os.Remove(lockfilePath)
}