Skip to content

Commit 7d654e0

Browse files
authored
Merge pull request go-mysql-org#1 from WangXiangUSTC/connect_timeout
add retry for connect
2 parents 05bb7dc + d38d291 commit 7d654e0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

client/conn.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,23 @@ func getNetProto(addr string) string {
5353

5454
// Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock.
5555
// Accepts a series of configuration functions as a variadic argument.
56-
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) {
56+
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (conn *Conn, err error) {
5757
proto := getNetProto(addr)
5858

59-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
60-
defer cancel()
61-
6259
dialer := &net.Dialer{}
6360

64-
return ConnectWithDialer(ctx, proto, addr, user, password, dbName, dialer.DialContext, options...)
61+
for i := 0; i < 10; i++ {
62+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
63+
conn, err = ConnectWithDialer(ctx, proto, addr, user, password, dbName, dialer.DialContext, options...)
64+
if err == nil {
65+
cancel()
66+
return
67+
}
68+
cancel()
69+
time.Sleep(time.Second * 1)
70+
}
71+
72+
return
6573
}
6674

6775
// Dialer connects to the address on the named network using the provided context.

0 commit comments

Comments
 (0)