Skip to content

Commit

Permalink
client:Fix DoTimeout timeout failure by setting temporary dial (#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukayiyi committed Apr 15, 2023
1 parent 43cc487 commit 87cb886
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ func (c *HostClient) acquireConn(reqTimeout time.Duration, connectionClose bool)
go c.connsCleaner()
}

conn, err := c.dialHostHard()
conn, err := c.dialHostHard(reqTimeout)
if err != nil {
c.decConnsCount()
return nil, err
Expand All @@ -1634,7 +1634,7 @@ func (c *HostClient) queueForIdle(w *wantConn) {
}

func (c *HostClient) dialConnFor(w *wantConn) {
conn, err := c.dialHostHard()
conn, err := c.dialHostHard(0)
if err != nil {
w.tryDeliver(nil, err)
c.decConnsCount()
Expand Down Expand Up @@ -1921,7 +1921,8 @@ func (c *HostClient) nextAddr() string {
return addr
}

func (c *HostClient) dialHostHard() (conn net.Conn, err error) {
func (c *HostClient) dialHostHard(dialTimeout time.Duration) (conn net.Conn, err error) {
// use dialTimeout to control the timeout of each dial. It does not work if dialTimeout is 0 or dial has been set.
// attempt to dial all the available hosts before giving up.

c.addrsLock.Lock()
Expand All @@ -1933,6 +1934,13 @@ func (c *HostClient) dialHostHard() (conn net.Conn, err error) {
n = 1
}

dial := c.Dial
if dialTimeout != 0 && dial == nil {
dial = func(addr string) (net.Conn, error) {
return DialTimeout(addr, dialTimeout)

This comment has been minimized.

Copy link
@cloudfly

cloudfly Aug 8, 2023

Contributor

We should consider the c.DialDualStack parameter here. If it is set to true, we should return DialDualStackTimeout instead of DialTimeout.

}
}

timeout := c.ReadTimeout + c.WriteTimeout
if timeout <= 0 {
timeout = DefaultDialTimeout
Expand All @@ -1941,7 +1949,7 @@ func (c *HostClient) dialHostHard() (conn net.Conn, err error) {
for n > 0 {
addr := c.nextAddr()
tlsConfig := c.cachedTLSConfig(addr)
conn, err = dialAddr(addr, c.Dial, c.DialDualStack, c.IsTLS, tlsConfig, c.WriteTimeout)
conn, err = dialAddr(addr, dial, c.DialDualStack, c.IsTLS, tlsConfig, c.WriteTimeout)

This comment has been minimized.

Copy link
@cloudfly

cloudfly Aug 8, 2023

Contributor

Look here, the c.DialDualStack will be ignored in the dialAddr method, because when dialTimeout != 0 && c.Dial == nil, dial cannot be nil anymore.

This comment has been minimized.

Copy link
@erikdubbelboer

erikdubbelboer Aug 8, 2023

Collaborator

Can you make a pull request to fix these issues?

if err == nil {
return conn, nil
}
Expand Down

0 comments on commit 87cb886

Please sign in to comment.