Set a 10 seconds timeout in Dial

This commit is contained in:
Alexandre Cesaro 2016-04-11 23:23:03 +02:00
parent 92eaa13340
commit e4bd87ad6e
2 changed files with 7 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"net/smtp" "net/smtp"
"strings" "strings"
"time"
) )
// A Dialer is a dialer to an SMTP server. // A Dialer is a dialer to an SMTP server.
@ -57,7 +58,7 @@ func NewPlainDialer(host string, port int, username, password string) *Dialer {
// Dial dials and authenticates to an SMTP server. The returned SendCloser // Dial dials and authenticates to an SMTP server. The returned SendCloser
// should be closed when done using it. // should be closed when done using it.
func (d *Dialer) Dial() (SendCloser, error) { func (d *Dialer) Dial() (SendCloser, error) {
conn, err := netDial("tcp", addr(d.Host, d.Port)) conn, err := netDialTimeout("tcp", addr(d.Host, d.Port), 10*time.Second)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -181,7 +182,7 @@ func (c *smtpSender) Close() error {
// Stubbed out for tests. // Stubbed out for tests.
var ( var (
netDial = net.Dial netDialTimeout = net.DialTimeout
tlsClient = tls.Client tlsClient = tls.Client
smtpNewClient = func(conn net.Conn, host string) (smtpClient, error) { smtpNewClient = func(conn net.Conn, host string) (smtpClient, error) {
return smtp.NewClient(conn, host) return smtp.NewClient(conn, host)

View File

@ -8,6 +8,7 @@ import (
"net/smtp" "net/smtp"
"reflect" "reflect"
"testing" "testing"
"time"
) )
const ( const (
@ -247,7 +248,7 @@ func doTestSendMail(t *testing.T, d *Dialer, want []string, timeout bool) {
timeout: timeout, timeout: timeout,
} }
netDial = func(network, address string) (net.Conn, error) { netDialTimeout = func(network, address string, d time.Duration) (net.Conn, error) {
if network != "tcp" { if network != "tcp" {
t.Errorf("Invalid network, got %q, want tcp", network) t.Errorf("Invalid network, got %q, want tcp", network)
} }