Skip to content

Commit 3f11740

Browse files
author
doganpoyraz
committed
Pass explicit timeout for custom dialers
1 parent 8b2892f commit 3f11740

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

client/client.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,10 @@ type Dialer interface {
629629
//
630630
// Among other uses, this allows to apply a dial timeout.
631631
func DialWithDialer(dialer Dialer, addr string) (*Client, error) {
632+
return DialWithDialerAndTimeout(dialer, addr, 0)
633+
}
634+
635+
func DialWithDialerAndTimeout(dialer Dialer, addr string, timeout time.Duration) (*Client, error) {
632636
conn, err := dialer.Dial("tcp", addr)
633637
if err != nil {
634638
return nil, err
@@ -638,13 +642,16 @@ func DialWithDialer(dialer Dialer, addr string) (*Client, error) {
638642
// there is no way to set the client's Timeout for that action. As a
639643
// workaround, if the dialer has a timeout set, use that for the connection's
640644
// deadline.
641-
var timeout time.Duration
642-
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
643-
err := conn.SetDeadline(time.Now().Add(netDialer.Timeout))
645+
if timeout == 0 {
646+
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
647+
timeout = netDialer.Timeout
648+
}
649+
}
650+
if timeout > 0 {
651+
err = conn.SetDeadline(time.Now().Add(timeout))
644652
if err != nil {
645653
return nil, err
646654
}
647-
timeout = netDialer.Timeout
648655
}
649656

650657
c, err := NewWithTimeout(conn, timeout)
@@ -666,6 +673,10 @@ func DialTLS(addr string, tlsConfig *tls.Config) (*Client, error) {
666673
//
667674
// Among other uses, this allows to apply a dial timeout.
668675
func DialWithDialerTLS(dialer Dialer, addr string, tlsConfig *tls.Config) (*Client, error) {
676+
return DialWithDialerAndTimeoutTLS(dialer, addr, tlsConfig, 0)
677+
}
678+
679+
func DialWithDialerAndTimeoutTLS(dialer Dialer, addr string, tlsConfig *tls.Config, timeout time.Duration) (*Client, error) {
669680
conn, err := dialer.Dial("tcp", addr)
670681
if err != nil {
671682
return nil, err
@@ -685,13 +696,16 @@ func DialWithDialerTLS(dialer Dialer, addr string, tlsConfig *tls.Config) (*Clie
685696
// there is no way to set the client's Timeout for that action. As a
686697
// workaround, if the dialer has a timeout set, use that for the connection's
687698
// deadline.
688-
var timeout time.Duration
689-
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
690-
err := tlsConn.SetDeadline(time.Now().Add(netDialer.Timeout))
699+
if timeout == 0 {
700+
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
701+
timeout = netDialer.Timeout
702+
}
703+
}
704+
if timeout > 0 {
705+
err = conn.SetDeadline(time.Now().Add(timeout))
691706
if err != nil {
692707
return nil, err
693708
}
694-
timeout = netDialer.Timeout
695709
}
696710

697711
c, err := NewWithTimeout(tlsConn, timeout)

0 commit comments

Comments
 (0)