Skip to content

Commit

Permalink
connectionTimeout respects net.Dialer default timeout (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzming authored Sep 15, 2023
1 parent f979c18 commit 7cf643b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 3 additions & 4 deletions pulsar/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
)

const (
defaultConnectionTimeout = 10 * time.Second
defaultOperationTimeout = 30 * time.Second
defaultKeepAliveInterval = 30 * time.Second
defaultMemoryLimitBytes = 64 * 1024 * 1024
Expand Down Expand Up @@ -117,10 +116,10 @@ func newClient(options ClientOptions) (Client, error) {
return nil, err
}

// the default timeout respects Go's default timeout which is no timeout
// Missing user specified timeout renders 0 values that matches
// net.Dailer's default if time.Duration value is not initialized
connectionTimeout := options.ConnectionTimeout
if connectionTimeout.Nanoseconds() == 0 {
connectionTimeout = defaultConnectionTimeout
}

operationTimeout := options.OperationTimeout
if operationTimeout.Nanoseconds() == 0 {
Expand Down
8 changes: 7 additions & 1 deletion pulsar/internal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ func (c *connection) connect() bool {

if c.tlsOptions == nil {
// Clear text connection
cnx, err = net.DialTimeout("tcp", c.physicalAddr.Host, c.connectionTimeout)
if c.connectionTimeout.Nanoseconds() > 0 {
cnx, err = net.DialTimeout("tcp", c.physicalAddr.Host, c.connectionTimeout)
} else {
cnx, err = net.Dial("tcp", c.physicalAddr.Host)
}
} else {
// TLS connection
tlsConfig, err = c.getTLSConfig()
Expand All @@ -265,6 +269,8 @@ func (c *connection) connect() bool {
return false
}

// time.Duration is initialized to 0 by default, net.Dialer's default timeout is no timeout
// therefore if c.connectionTimeout is 0, it means no timeout
d := &net.Dialer{Timeout: c.connectionTimeout}
cnx, err = tls.DialWithDialer(d, "tcp", c.physicalAddr.Host, tlsConfig)
}
Expand Down

0 comments on commit 7cf643b

Please sign in to comment.