Skip to content

Commit 2a2babe

Browse files
authored
Merge pull request #321 from martin-sucha/issue-316
consumer: fix panic in nextLookupdEndpoint
2 parents 63a3a23 + 0496cd4 commit 2a2babe

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

consumer.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,21 @@ func (r *Consumer) ConnectToNSQLookupd(addr string) error {
340340
return errors.New("no handlers")
341341
}
342342

343-
if err := validatedLookupAddr(addr); err != nil {
343+
parsedAddr, err := buildLookupAddr(addr, r.topic)
344+
if err != nil {
344345
return err
345346
}
346347

347348
atomic.StoreInt32(&r.connectedFlag, 1)
348349

349350
r.mtx.Lock()
350351
for _, x := range r.lookupdHTTPAddrs {
351-
if x == addr {
352+
if x == parsedAddr {
352353
r.mtx.Unlock()
353354
return nil
354355
}
355356
}
356-
r.lookupdHTTPAddrs = append(r.lookupdHTTPAddrs, addr)
357+
r.lookupdHTTPAddrs = append(r.lookupdHTTPAddrs, parsedAddr)
357358
numLookupd := len(r.lookupdHTTPAddrs)
358359
r.mtx.Unlock()
359360

@@ -383,20 +384,6 @@ func (r *Consumer) ConnectToNSQLookupds(addresses []string) error {
383384
return nil
384385
}
385386

386-
func validatedLookupAddr(addr string) error {
387-
if strings.Contains(addr, "/") {
388-
_, err := url.Parse(addr)
389-
if err != nil {
390-
return err
391-
}
392-
return nil
393-
}
394-
if !strings.Contains(addr, ":") {
395-
return errors.New("missing port")
396-
}
397-
return nil
398-
}
399-
400387
// poll all known lookup servers every LookupdPollInterval
401388
func (r *Consumer) lookupdLoop() {
402389
// add some jitter so that multiple consumers discovering the same topic,
@@ -446,23 +433,7 @@ func (r *Consumer) nextLookupdEndpoint() string {
446433
r.mtx.RUnlock()
447434
r.lookupdQueryIndex = (r.lookupdQueryIndex + 1) % num
448435

449-
urlString := addr
450-
if !strings.Contains(urlString, "://") {
451-
urlString = "http://" + addr
452-
}
453-
454-
u, err := url.Parse(urlString)
455-
if err != nil {
456-
panic(err)
457-
}
458-
if u.Path == "/" || u.Path == "" {
459-
u.Path = "/lookup"
460-
}
461-
462-
v, err := url.ParseQuery(u.RawQuery)
463-
v.Add("topic", r.topic)
464-
u.RawQuery = v.Encode()
465-
return u.String()
436+
return addr
466437
}
467438

468439
type lookupResp struct {
@@ -659,10 +630,15 @@ func (r *Consumer) DisconnectFromNSQD(addr string) error {
659630
// DisconnectFromNSQLookupd removes the specified `nsqlookupd` address
660631
// from the list used for periodic discovery.
661632
func (r *Consumer) DisconnectFromNSQLookupd(addr string) error {
633+
parsedAddr, err := buildLookupAddr(addr, r.topic)
634+
if err != nil {
635+
return err
636+
}
637+
662638
r.mtx.Lock()
663639
defer r.mtx.Unlock()
664640

665-
idx := indexOf(addr, r.lookupdHTTPAddrs)
641+
idx := indexOf(parsedAddr, r.lookupdHTTPAddrs)
666642
if idx == -1 {
667643
return ErrNotConnected
668644
}
@@ -1204,3 +1180,28 @@ func (r *Consumer) log(lvl LogLevel, line string, args ...interface{}) {
12041180
lvl, r.id, r.topic, r.channel,
12051181
fmt.Sprintf(line, args...)))
12061182
}
1183+
1184+
func buildLookupAddr(addr, topic string) (string, error) {
1185+
urlString := addr
1186+
if !strings.Contains(urlString, "://") {
1187+
urlString = "http://" + addr
1188+
}
1189+
1190+
u, err := url.Parse(urlString)
1191+
if err != nil {
1192+
return "", err
1193+
}
1194+
1195+
if u.Port() == "" {
1196+
return "", errors.New("missing port")
1197+
}
1198+
1199+
if u.Path == "/" || u.Path == "" {
1200+
u.Path = "/lookup"
1201+
}
1202+
1203+
v, err := url.ParseQuery(u.RawQuery)
1204+
v.Add("topic", topic)
1205+
u.RawQuery = v.Encode()
1206+
return u.String(), nil
1207+
}

0 commit comments

Comments
 (0)