Skip to content

Commit c1362f3

Browse files
committed
nsqd: flock --data-path for unix-like platforms
1 parent ba7c4b4 commit c1362f3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

internal/dirlock/dirlock.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// +build !windows
2+
3+
package dirlock
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"syscall"
9+
)
10+
11+
type DirLock struct {
12+
dir string
13+
f *os.File
14+
}
15+
16+
func New(dir string) *DirLock {
17+
return &DirLock{
18+
dir: dir,
19+
}
20+
}
21+
22+
func (l *DirLock) Lock() error {
23+
f, err := os.Open(l.dir)
24+
if err != nil {
25+
return err
26+
}
27+
l.f = f
28+
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
29+
if err != nil {
30+
return fmt.Errorf("cannot flock directory %s - %s", l.dir, err)
31+
}
32+
return nil
33+
}
34+
35+
func (l *DirLock) Unlock() error {
36+
defer l.f.Close()
37+
return syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN)
38+
}

internal/dirlock/dirlock_windows.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build windows
2+
3+
package dirlock
4+
5+
type DirLock struct {
6+
dir string
7+
}
8+
9+
func New(dir string) *DirLock {
10+
return &DirLock{
11+
dir: dir,
12+
}
13+
}
14+
15+
func (l *DirLock) Lock() error {
16+
return nil
17+
}
18+
19+
func (l *DirLock) Unlock() error {
20+
return nil
21+
}

nsqd/nsqd.go

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/bitly/go-simplejson"
2121
"github.com/nsqio/nsq/internal/clusterinfo"
22+
"github.com/nsqio/nsq/internal/dirlock"
2223
"github.com/nsqio/nsq/internal/http_api"
2324
"github.com/nsqio/nsq/internal/protocol"
2425
"github.com/nsqio/nsq/internal/statsd"
@@ -45,6 +46,7 @@ type NSQD struct {
4546

4647
opts atomic.Value
4748

49+
dl *dirlock.DirLock
4850
flag int32
4951
errMtx sync.RWMutex
5052
err error
@@ -71,6 +73,12 @@ type NSQD struct {
7173
}
7274

7375
func New(opts *Options) *NSQD {
76+
dataPath := opts.DataPath
77+
if opts.DataPath == "" {
78+
cwd, _ := os.Getwd()
79+
dataPath = cwd
80+
}
81+
7482
n := &NSQD{
7583
flag: flagHealthy,
7684
startTime: time.Now(),
@@ -80,9 +88,16 @@ func New(opts *Options) *NSQD {
8088
notifyChan: make(chan interface{}),
8189
optsNotificationChan: make(chan struct{}, 1),
8290
ci: clusterinfo.New(opts.Logger, http_api.NewClient(nil)),
91+
dl: dirlock.New(dataPath),
8392
}
8493
n.swapOpts(opts)
8594

95+
err := n.dl.Lock()
96+
if err != nil {
97+
n.logf("FATAL: --data-path=%s in use (possibly by another instance of nsqd)", dataPath)
98+
os.Exit(1)
99+
}
100+
86101
if opts.MaxDeflateLevel < 1 || opts.MaxDeflateLevel > 9 {
87102
n.logf("FATAL: --max-deflate-level must be [1,9]")
88103
os.Exit(1)
@@ -435,6 +450,8 @@ func (n *NSQD) Exit() {
435450
// could potentially starve items in process and deadlock)
436451
close(n.exitChan)
437452
n.waitGroup.Wait()
453+
454+
n.dl.Unlock()
438455
}
439456

440457
// GetTopic performs a thread safe operation

0 commit comments

Comments
 (0)