Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto expand with multiple hosts #410

Merged
merged 3 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
DefaultPort = 4222
DefaultMaxReconnect = 60
DefaultReconnectWait = 2 * time.Second
DefaultTimeout = 2 * time.Second
DefaultTimeout = 1 * time.Second
DefaultPingInterval = 2 * time.Minute
DefaultMaxPingOut = 2
DefaultMaxChanLen = 8192 // 8k
Expand Down Expand Up @@ -877,8 +877,8 @@ func (nc *Conn) SetErrorHandler(cb ErrHandler) {
nc.Opts.AsyncErrorCB = cb
}

// Process the url string argument to Connect. Return an array of
// urls, even if only one.
// Process the url string argument to Connect.
// Return an array of urls, even if only one.
func processUrlString(url string) []string {
urls := strings.Split(url, ",")
for i, s := range urls {
Expand Down Expand Up @@ -1111,6 +1111,7 @@ func (nc *Conn) addURLToPool(sURL string, implicit, saveTLSName bool) error {
}
sURL += defaultPortString
}

var tlsName string
if implicit {
curl := nc.current.url
Expand Down Expand Up @@ -1167,14 +1168,38 @@ func (nc *Conn) createConn() (err error) {
cur.lastAttempt = time.Now()
}

// We will auto-expand host names if they resolve to multiple IPs
var hosts []string
u := nc.current.url

if net.ParseIP(u.Hostname()) == nil {
addrs, _ := net.LookupHost(u.Hostname())
for _, addr := range addrs {
hosts = append(hosts, fmt.Sprintf("%s:%s", addr, u.Port()))
}
}
// Fall back to what we were given.
if len(hosts) == 0 {
hosts = append(hosts, u.Host)
}

// CustomDialer takes precedence. If not set, use Opts.Dialer which
// is set to a default *net.Dialer (in Connect()) if not explicitly
// set by the user.
dialer := nc.Opts.CustomDialer
if dialer == nil {
dialer = nc.Opts.Dialer
// We will copy and shorten the timeout if we have multiple hosts to try.
copyDialer := *nc.Opts.Dialer
kozlovic marked this conversation as resolved.
Show resolved Hide resolved
copyDialer.Timeout = copyDialer.Timeout / time.Duration(len(hosts))
dialer = &copyDialer
}

for _, host := range hosts {
nc.conn, err = dialer.Dial("tcp", host)
if err == nil {
break
}
}
nc.conn, err = dialer.Dial("tcp", nc.current.url.Host)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ func TestUseCustomDialer(t *testing.T) {
}

// Create custom dialer that return error on Dial().
cdialer := &customDialer{ch: make(chan bool, 1)}
cdialer := &customDialer{ch: make(chan bool, 10)}

// When both Dialer and CustomDialer are set, CustomDialer
// should take precedence. That means that the connection
Expand Down