Skip to content

Commit 86af3ca

Browse files
committed
nsqd: cover additional edge cases
1 parent baa3e4b commit 86af3ca

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

nsqd/http.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,12 @@ func (s *httpServer) getTopicFromQuery(req *http.Request) (url.Values, *Topic, e
180180
if !protocol.IsValidTopicName(topicName) {
181181
return nil, nil, http_api.Err{400, "INVALID_TOPIC"}
182182
}
183+
topic := s.ctx.nsqd.GetTopic(topicName)
184+
if topic == nil {
185+
return nil, nil, http_api.Err{503, "EXITING"}
186+
}
183187

184-
return reqParams, s.nsqd.GetTopic(topicName), nil
188+
return reqParams, topic, nil
185189
}
186190

187191
func (s *httpServer) doPUB(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
@@ -431,7 +435,10 @@ func (s *httpServer) doCreateChannel(w http.ResponseWriter, req *http.Request, p
431435
if err != nil {
432436
return nil, err
433437
}
434-
topic.GetChannel(channelName)
438+
ch := topic.GetChannel(channelName)
439+
if ch == nil {
440+
return nil, http_api.Err{503, "EXITING"}
441+
}
435442
return nil, nil
436443
}
437444

nsqd/nsqd.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,21 @@ func (n *NSQD) GetTopic(topicName string) *Topic {
478478
n.Unlock()
479479
return t
480480
}
481+
if atomic.LoadInt32(&t.isDraining) == 1 {
482+
// don't create new topics when nsqd is draining
483+
return nil
484+
}
485+
481486
deleteCallback := func(t *Topic) {
482487
n.DeleteExistingTopic(t.name)
488+
if atomic.LoadInt32(&t.isDraining) == 1 {
489+
n.RLock()
490+
topicCount := len(n.topicMap)
491+
n.RUnlock()
492+
if topicCount == 0 {
493+
n.Exit()
494+
}
495+
}
483496
}
484497
t = NewTopic(topicName, n, deleteCallback)
485498
n.topicMap[topicName] = t
@@ -563,6 +576,9 @@ func (n *NSQD) StartDraining() error {
563576
for _, t := range n.topicMap {
564577
t.StartDraining()
565578
}
579+
if len(n.topicMap) == 0 {
580+
n.Exit()
581+
}
566582
return nil
567583
}
568584

0 commit comments

Comments
 (0)