Skip to content

Commit 8b2892f

Browse files
author
doganpoyraz
committed
Fix hanging connection during client construction
Set timeout value to the client to supply deadline values to Support calls in the client.New function
1 parent 999137e commit 8b2892f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

client/client.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ func (c *Client) SetDebug(w io.Writer) {
581581

582582
// New creates a new client from an existing connection.
583583
func New(conn net.Conn) (*Client, error) {
584+
return NewWithTimeout(conn, 0)
585+
}
586+
587+
// New creates a new client from an existing connection with a timeout.
588+
func NewWithTimeout(conn net.Conn, timeout time.Duration) (*Client, error) {
584589
continues := make(chan bool)
585590
w := imap.NewClientWriter(nil, continues)
586591
r := imap.NewReader(nil)
@@ -591,6 +596,7 @@ func New(conn net.Conn) (*Client, error) {
591596
continues: continues,
592597
state: imap.ConnectingState,
593598
ErrorLog: log.New(os.Stderr, "imap/client: ", log.LstdFlags),
599+
Timeout: timeout,
594600
}
595601

596602
c.handleContinuationReqs()
@@ -632,14 +638,16 @@ func DialWithDialer(dialer Dialer, addr string) (*Client, error) {
632638
// there is no way to set the client's Timeout for that action. As a
633639
// workaround, if the dialer has a timeout set, use that for the connection's
634640
// deadline.
641+
var timeout time.Duration
635642
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
636643
err := conn.SetDeadline(time.Now().Add(netDialer.Timeout))
637644
if err != nil {
638645
return nil, err
639646
}
647+
timeout = netDialer.Timeout
640648
}
641649

642-
c, err := New(conn)
650+
c, err := NewWithTimeout(conn, timeout)
643651
if err != nil {
644652
return nil, err
645653
}
@@ -677,14 +685,16 @@ func DialWithDialerTLS(dialer Dialer, addr string, tlsConfig *tls.Config) (*Clie
677685
// there is no way to set the client's Timeout for that action. As a
678686
// workaround, if the dialer has a timeout set, use that for the connection's
679687
// deadline.
688+
var timeout time.Duration
680689
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
681690
err := tlsConn.SetDeadline(time.Now().Add(netDialer.Timeout))
682691
if err != nil {
683692
return nil, err
684693
}
694+
timeout = netDialer.Timeout
685695
}
686696

687-
c, err := New(tlsConn)
697+
c, err := NewWithTimeout(tlsConn, timeout)
688698
if err != nil {
689699
return nil, err
690700
}

0 commit comments

Comments
 (0)