@@ -19,6 +19,7 @@ import (
19
19
20
20
"github.com/bitly/go-simplejson"
21
21
"github.com/nsqio/nsq/internal/clusterinfo"
22
+ "github.com/nsqio/nsq/internal/dirlock"
22
23
"github.com/nsqio/nsq/internal/http_api"
23
24
"github.com/nsqio/nsq/internal/protocol"
24
25
"github.com/nsqio/nsq/internal/statsd"
@@ -45,6 +46,7 @@ type NSQD struct {
45
46
46
47
opts atomic.Value
47
48
49
+ dl * dirlock.DirLock
48
50
flag int32
49
51
errMtx sync.RWMutex
50
52
err error
@@ -71,6 +73,12 @@ type NSQD struct {
71
73
}
72
74
73
75
func New (opts * Options ) * NSQD {
76
+ dataPath := opts .DataPath
77
+ if opts .DataPath == "" {
78
+ cwd , _ := os .Getwd ()
79
+ dataPath = cwd
80
+ }
81
+
74
82
n := & NSQD {
75
83
flag : flagHealthy ,
76
84
startTime : time .Now (),
@@ -80,9 +88,16 @@ func New(opts *Options) *NSQD {
80
88
notifyChan : make (chan interface {}),
81
89
optsNotificationChan : make (chan struct {}, 1 ),
82
90
ci : clusterinfo .New (opts .Logger , http_api .NewClient (nil )),
91
+ dl : dirlock .New (dataPath ),
83
92
}
84
93
n .swapOpts (opts )
85
94
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
+
86
101
if opts .MaxDeflateLevel < 1 || opts .MaxDeflateLevel > 9 {
87
102
n .logf ("FATAL: --max-deflate-level must be [1,9]" )
88
103
os .Exit (1 )
@@ -435,6 +450,8 @@ func (n *NSQD) Exit() {
435
450
// could potentially starve items in process and deadlock)
436
451
close (n .exitChan )
437
452
n .waitGroup .Wait ()
453
+
454
+ n .dl .Unlock ()
438
455
}
439
456
440
457
// GetTopic performs a thread safe operation
0 commit comments