@@ -581,6 +581,11 @@ func (c *Client) SetDebug(w io.Writer) {
581581
582582// New creates a new client from an existing connection.
583583func 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