Skip to content

Commit cd671ca

Browse files
bouktz70s
authored andcommitted
connector: don't return ErrBadConn when failing to connect (go-sql-driver#1020)
ErrBadConn should only be returned for an already established connection, not when creating a new one.
1 parent e67527c commit cd671ca

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Zhenye Xie <xiezhenye at gmail.com>
9191

9292
Barracuda Networks, Inc.
9393
Counting Ltd.
94+
DigitalOcean Inc.
9495
Facebook Inc.
9596
GitHub Inc.
9697
Google Inc.

connector.go

-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
4444
}
4545

4646
if err != nil {
47-
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
48-
errLog.Print("net.Error from Dial()': ", nerr.Error())
49-
return nil, driver.ErrBadConn
50-
}
5147
return nil, err
5248
}
5349

connector_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package mysql
2+
3+
import (
4+
"context"
5+
"net"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestConnectorReturnsTimeout(t *testing.T) {
11+
connector := &connector{&Config{
12+
Net: "tcp",
13+
Addr: "1.1.1.1:1234",
14+
Timeout: 10 * time.Millisecond,
15+
}}
16+
17+
_, err := connector.Connect(context.Background())
18+
if err == nil {
19+
t.Fatal("error expected")
20+
}
21+
22+
if nerr, ok := err.(*net.OpError); ok {
23+
expected := "dial tcp 1.1.1.1:1234: i/o timeout"
24+
if nerr.Error() != expected {
25+
t.Fatalf("expected %q, got %q", expected, nerr.Error())
26+
}
27+
} else {
28+
t.Fatalf("expected %T, got %T", nerr, err)
29+
}
30+
}

driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ func TestDialNonRetryableNetErr(t *testing.T) {
18741874

18751875
func TestDialTemporaryNetErr(t *testing.T) {
18761876
testErr := netErrorMock{temporary: true}
1877-
testDialError(t, testErr, driver.ErrBadConn)
1877+
testDialError(t, testErr, testErr)
18781878
}
18791879

18801880
// Tests custom dial functions

0 commit comments

Comments
 (0)