Skip to content

Commit 958aa76

Browse files
authored
Merge pull request #643 from bakins/client-dialer
Add ConnectWithDialer
2 parents 89f35c0 + 58dc7a4 commit 958aa76

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

client/conn.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"context"
45
"crypto/tls"
56
"fmt"
67
"net"
@@ -55,10 +56,23 @@ func getNetProto(addr string) string {
5556
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) {
5657
proto := getNetProto(addr)
5758

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) {
5872
c := new(Conn)
5973

6074
var err error
61-
conn, err := net.DialTimeout(proto, addr, 10*time.Second)
75+
conn, err := dialer(ctx, network, addr)
6276
if err != nil {
6377
return nil, errors.Trace(err)
6478
}
@@ -72,9 +86,9 @@ func Connect(addr string, user string, password string, dbName string, options .
7286
c.user = user
7387
c.password = password
7488
c.db = dbName
75-
c.proto = proto
89+
c.proto = network
7690

77-
//use default charset here, utf-8
91+
// use default charset here, utf-8
7892
c.charset = DEFAULT_CHARSET
7993

8094
// Apply configuration functions.

0 commit comments

Comments
 (0)