Skip to content

Commit 31d874d

Browse files
committed
Specify a custom dial function per config
1 parent 0004702 commit 31d874d

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

connector.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,30 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
7777
mc.parseTime = mc.cfg.ParseTime
7878

7979
// Connect to Server
80-
dialsLock.RLock()
81-
dial, ok := dials[mc.cfg.Net]
82-
dialsLock.RUnlock()
83-
if ok {
80+
if c.cfg.DialFunc != nil {
8481
dctx := ctx
8582
if mc.cfg.Timeout > 0 {
8683
var cancel context.CancelFunc
8784
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
8885
defer cancel()
8986
}
90-
mc.netConn, err = dial(dctx, mc.cfg.Addr)
87+
mc.netConn, err = c.cfg.DialFunc(dctx, mc.cfg.Net, mc.cfg.Addr)
9188
} else {
92-
nd := net.Dialer{Timeout: mc.cfg.Timeout}
93-
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
89+
dialsLock.RLock()
90+
dial, ok := dials[mc.cfg.Net]
91+
dialsLock.RUnlock()
92+
if ok {
93+
dctx := ctx
94+
if mc.cfg.Timeout > 0 {
95+
var cancel context.CancelFunc
96+
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
97+
defer cancel()
98+
}
99+
mc.netConn, err = dial(dctx, mc.cfg.Addr)
100+
} else {
101+
nd := net.Dialer{Timeout: mc.cfg.Timeout}
102+
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
103+
}
94104
}
95105

96106
if err != nil {

dsn.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package mysql
1010

1111
import (
1212
"bytes"
13+
"context"
1314
"crypto/rsa"
1415
"crypto/tls"
1516
"errors"
@@ -34,24 +35,25 @@ var (
3435
// If a new Config is created instead of being parsed from a DSN string,
3536
// the NewConfig function should be used, which sets default values.
3637
type Config struct {
37-
User string // Username
38-
Passwd string // Password (requires User)
39-
Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
40-
Addr string // Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
41-
DBName string // Database name
42-
Params map[string]string // Connection parameters
43-
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
44-
Collation string // Connection collation
45-
Loc *time.Location // Location for time.Time values
46-
MaxAllowedPacket int // Max packet size allowed
47-
ServerPubKey string // Server public key name
48-
pubKey *rsa.PublicKey // Server public key
49-
TLSConfig string // TLS configuration name
50-
TLS *tls.Config // TLS configuration, its priority is higher than TLSConfig
51-
Timeout time.Duration // Dial timeout
52-
ReadTimeout time.Duration // I/O read timeout
53-
WriteTimeout time.Duration // I/O write timeout
54-
Logger Logger // Logger
38+
User string // Username
39+
Passwd string // Password (requires User)
40+
Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
41+
Addr string // Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
42+
DBName string // Database name
43+
Params map[string]string // Connection parameters
44+
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
45+
Collation string // Connection collation
46+
Loc *time.Location // Location for time.Time values
47+
MaxAllowedPacket int // Max packet size allowed
48+
ServerPubKey string // Server public key name
49+
pubKey *rsa.PublicKey // Server public key
50+
TLSConfig string // TLS configuration name
51+
TLS *tls.Config // TLS configuration, its priority is higher than TLSConfig
52+
Timeout time.Duration // Dial timeout
53+
ReadTimeout time.Duration // I/O read timeout
54+
WriteTimeout time.Duration // I/O write timeout
55+
Logger Logger // Logger
56+
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error) // Specifies the dial function for creating connections
5557

5658
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
5759
AllowCleartextPasswords bool // Allows the cleartext client side plugin

0 commit comments

Comments
 (0)