mmm: lockfile to prevent multiple instances.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user