-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nsqd: flock --data-path for unix-like platforms
- Loading branch information
1 parent
ba7c4b4
commit 2505202
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// +build !windows | ||
|
||
package dirlock | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
type DirLock struct { | ||
dir string | ||
f *os.File | ||
} | ||
|
||
func New(dir string) *DirLock { | ||
return &DirLock{ | ||
dir: dir, | ||
} | ||
} | ||
|
||
func (l *DirLock) Lock() error { | ||
f, err := os.Open(l.dir) | ||
if err != nil { | ||
return err | ||
} | ||
l.f = f | ||
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) | ||
if err != nil { | ||
return fmt.Errorf("ERROR: cannot flock directory %s - %s", l.dir, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (l *DirLock) Unlock() error { | ||
defer l.f.Close() | ||
return syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build windows | ||
|
||
package dirlock | ||
|
||
type DirLock struct { | ||
dir string | ||
} | ||
|
||
func New(dir string) *DirLock { | ||
return &DirLock{ | ||
dir: dir, | ||
} | ||
} | ||
|
||
func (l *DirLock) Lock() error { | ||
return nil | ||
} | ||
|
||
func (l *DirLock) Unlock() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters