1
1
package client
2
2
3
3
import (
4
+ "context"
4
5
"crypto/tls"
5
6
"fmt"
6
7
"net"
@@ -55,10 +56,23 @@ func getNetProto(addr string) string {
55
56
func Connect (addr string , user string , password string , dbName string , options ... func (* Conn )) (* Conn , error ) {
56
57
proto := getNetProto (addr )
57
58
59
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
60
+ defer cancel ()
61
+
62
+ dialer := & net.Dialer {}
63
+
64
+ return ConnectWithDialer (ctx , proto , addr , user , password , dbName , dialer .DialContext , options ... )
65
+ }
66
+
67
+ // Dialer connects to the address on the named network using the provided context.
68
+ type Dialer func (ctx context.Context , network , address string ) (net.Conn , error )
69
+
70
+ // Connect to a MySQL server using the given Dialer.
71
+ func ConnectWithDialer (ctx context.Context , network string , addr string , user string , password string , dbName string , dialer Dialer , options ... func (* Conn )) (* Conn , error ) {
58
72
c := new (Conn )
59
73
60
74
var err error
61
- conn , err := net . DialTimeout ( proto , addr , 10 * time . Second )
75
+ conn , err := dialer ( ctx , network , addr )
62
76
if err != nil {
63
77
return nil , errors .Trace (err )
64
78
}
@@ -72,9 +86,9 @@ func Connect(addr string, user string, password string, dbName string, options .
72
86
c .user = user
73
87
c .password = password
74
88
c .db = dbName
75
- c .proto = proto
89
+ c .proto = network
76
90
77
- //use default charset here, utf-8
91
+ // use default charset here, utf-8
78
92
c .charset = DEFAULT_CHARSET
79
93
80
94
// Apply configuration functions.
0 commit comments