@@ -629,6 +629,10 @@ type Dialer interface {
629
629
//
630
630
// Among other uses, this allows to apply a dial timeout.
631
631
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 ) {
632
636
conn , err := dialer .Dial ("tcp" , addr )
633
637
if err != nil {
634
638
return nil , err
@@ -638,13 +642,16 @@ func DialWithDialer(dialer Dialer, addr string) (*Client, error) {
638
642
// there is no way to set the client's Timeout for that action. As a
639
643
// workaround, if the dialer has a timeout set, use that for the connection's
640
644
// 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 ))
644
652
if err != nil {
645
653
return nil , err
646
654
}
647
- timeout = netDialer .Timeout
648
655
}
649
656
650
657
c , err := NewWithTimeout (conn , timeout )
@@ -666,6 +673,10 @@ func DialTLS(addr string, tlsConfig *tls.Config) (*Client, error) {
666
673
//
667
674
// Among other uses, this allows to apply a dial timeout.
668
675
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 ) {
669
680
conn , err := dialer .Dial ("tcp" , addr )
670
681
if err != nil {
671
682
return nil , err
@@ -685,13 +696,16 @@ func DialWithDialerTLS(dialer Dialer, addr string, tlsConfig *tls.Config) (*Clie
685
696
// there is no way to set the client's Timeout for that action. As a
686
697
// workaround, if the dialer has a timeout set, use that for the connection's
687
698
// 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 ))
691
706
if err != nil {
692
707
return nil , err
693
708
}
694
- timeout = netDialer .Timeout
695
709
}
696
710
697
711
c , err := NewWithTimeout (tlsConn , timeout )
0 commit comments