Skip to content

Commit

Permalink
nsqadmin: Fix handling of IPv6 broadcast addresses
Browse files Browse the repository at this point in the history
Fixes nsqio#815
  • Loading branch information
magnetised authored and dolfly committed Jul 27, 2017
1 parent 0e61d05 commit b66580d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
47 changes: 47 additions & 0 deletions internal/clusterinfo/producer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package clusterinfo

import "testing"

func TestHostNameAddresses(t *testing.T) {
p := &Producer{
BroadcastAddress: "host.domain.com",
TCPPort: 4150,
HTTPPort: 4151,
}

if p.HTTPAddress() != "host.domain.com:4151" {
t.Errorf("Incorrect HTTPAddress: %s", p.HTTPAddress())
}
if p.TCPAddress() != "host.domain.com:4150" {
t.Errorf("Incorrect TCPAddress: %s", p.TCPAddress())
}
}

func TestIPv4Addresses(t *testing.T) {
p := &Producer{
BroadcastAddress: "192.168.1.17",
TCPPort: 4150,
HTTPPort: 4151,
}

if p.HTTPAddress() != "192.168.1.17:4151" {
t.Errorf("Incorrect IPv4 HTTPAddress: %s", p.HTTPAddress())
}
if p.TCPAddress() != "192.168.1.17:4150" {
t.Errorf("Incorrect IPv4 TCPAddress: %s", p.TCPAddress())
}
}

func TestIPv6Addresses(t *testing.T) {
p := &Producer{
BroadcastAddress: "fd4a:622f:d2f2::1",
TCPPort: 4150,
HTTPPort: 4151,
}
if p.HTTPAddress() != "[fd4a:622f:d2f2::1]:4151" {
t.Errorf("Incorrect IPv6 HTTPAddress: %s", p.HTTPAddress())
}
if p.TCPAddress() != "[fd4a:622f:d2f2::1]:4150" {
t.Errorf("Incorrect IPv6 TCPAddress: %s", p.TCPAddress())
}
}
6 changes: 4 additions & 2 deletions internal/clusterinfo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package clusterinfo
import (
"encoding/json"
"fmt"
"net"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -77,11 +79,11 @@ func (p *Producer) Address() string {
}

func (p *Producer) HTTPAddress() string {
return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.HTTPPort)
return net.JoinHostPort(p.BroadcastAddress, strconv.Itoa(p.HTTPPort))
}

func (p *Producer) TCPAddress() string {
return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.TCPPort)
return net.JoinHostPort(p.BroadcastAddress, strconv.Itoa(p.TCPPort))
}

// IsInconsistent checks for cases where an unexpected number of nsqd connections are
Expand Down

0 comments on commit b66580d

Please sign in to comment.