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‎

Lines changed: 17 additions & 7 deletions
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-
ifok{
80+
ifc.cfg.DialFunc!=nil{
8481
dctx:=ctx
8582
ifmc.cfg.Timeout>0{
8683
varcancel context.CancelFunc
8784
dctx, cancel=context.WithTimeout(ctx, c.cfg.Timeout)
8885
defercancel()
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+
ifok{
93+
dctx:=ctx
94+
ifmc.cfg.Timeout>0{
95+
varcancel context.CancelFunc
96+
dctx, cancel=context.WithTimeout(ctx, c.cfg.Timeout)
97+
defercancel()
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
iferr!=nil{

‎dsn.go‎

Lines changed: 20 additions & 18 deletions
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
typeConfigstruct{
37-
Userstring// Username
38-
Passwdstring// Password (requires User)
39-
Netstring// Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
40-
Addrstring// Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
41-
DBNamestring// Database name
42-
Paramsmap[string]string// Connection parameters
43-
ConnectionAttributesstring// Connection Attributes, comma-delimited string of user-defined "key:value" pairs
44-
Collationstring// Connection collation
45-
Loc*time.Location// Location for time.Time values
46-
MaxAllowedPacketint// Max packet size allowed
47-
ServerPubKeystring// Server public key name
48-
pubKey*rsa.PublicKey// Server public key
49-
TLSConfigstring// 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-
LoggerLogger// Logger
38+
Userstring// Username
39+
Passwdstring// Password (requires User)
40+
Netstring// Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
41+
Addrstring// Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
42+
DBNamestring// Database name
43+
Paramsmap[string]string// Connection parameters
44+
ConnectionAttributesstring// Connection Attributes, comma-delimited string of user-defined "key:value" pairs
45+
Collationstring// Connection collation
46+
Loc*time.Location// Location for time.Time values
47+
MaxAllowedPacketint// Max packet size allowed
48+
ServerPubKeystring// Server public key name
49+
pubKey*rsa.PublicKey// Server public key
50+
TLSConfigstring// 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+
LoggerLogger// Logger
56+
DialFuncfunc(ctx context.Context, network, addrstring) (net.Conn, error) // Specifies the dial function for creating connections
5557

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

0 commit comments

Comments
(0)