Skip to content

Commit 63f0c4b

Browse files
committed
Specify a custom dial function per config
1 parent 00dc21a commit 63f0c4b

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

‎connector.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,30 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error){
8787
mc.parseTime=mc.cfg.ParseTime
8888

8989
// Connect to Server
90-
dialsLock.RLock()
91-
dial, ok:=dials[mc.cfg.Net]
92-
dialsLock.RUnlock()
93-
ifok{
90+
ifc.cfg.DialFunc!=nil{
9491
dctx:=ctx
9592
ifmc.cfg.Timeout>0{
9693
varcancel context.CancelFunc
9794
dctx, cancel=context.WithTimeout(ctx, c.cfg.Timeout)
9895
defercancel()
9996
}
100-
mc.netConn, err=dial(dctx, mc.cfg.Addr)
97+
mc.netConn, err=c.cfg.DialFunc(dctx, mc.cfg.Net, mc.cfg.Addr)
10198
} else{
102-
nd:= net.Dialer{Timeout: mc.cfg.Timeout}
103-
mc.netConn, err=nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
99+
dialsLock.RLock()
100+
dial, ok:=dials[mc.cfg.Net]
101+
dialsLock.RUnlock()
102+
ifok{
103+
dctx:=ctx
104+
ifmc.cfg.Timeout>0{
105+
varcancel context.CancelFunc
106+
dctx, cancel=context.WithTimeout(ctx, c.cfg.Timeout)
107+
defercancel()
108+
}
109+
mc.netConn, err=dial(dctx, mc.cfg.Addr)
110+
} else{
111+
nd:= net.Dialer{Timeout: mc.cfg.Timeout}
112+
mc.netConn, err=nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
113+
}
104114
}
105115
iferr!=nil{
106116
returnnil, err

‎dsn.go‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,25 @@ var (
3737
typeConfigstruct{
3838
// non boolean fields
3939

40-
Userstring// Username
41-
Passwdstring// Password (requires User)
42-
Netstring// Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
43-
Addrstring// Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
44-
DBNamestring// Database name
45-
Paramsmap[string]string// Connection parameters
46-
ConnectionAttributesstring// Connection Attributes, comma-delimited string of user-defined "key:value" pairs
47-
charsets []string// Connection charset. When set, this will be set in SET NAMES <charset> query
48-
Collationstring// Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query
49-
Loc*time.Location// Location for time.Time values
50-
MaxAllowedPacketint// Max packet size allowed
51-
ServerPubKeystring// Server public key name
52-
TLSConfigstring// TLS configuration name
53-
TLS*tls.Config// TLS configuration, its priority is higher than TLSConfig
54-
Timeout time.Duration// Dial timeout
55-
ReadTimeout time.Duration// I/O read timeout
56-
WriteTimeout time.Duration// I/O write timeout
57-
LoggerLogger// Logger
40+
Userstring// Username
41+
Passwdstring// Password (requires User)
42+
Netstring// Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
43+
Addrstring// Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
44+
DBNamestring// Database name
45+
Paramsmap[string]string// Connection parameters
46+
ConnectionAttributesstring// Connection Attributes, comma-delimited string of user-defined "key:value" pairs
47+
charsets []string// Connection charset. When set, this will be set in SET NAMES <charset> query
48+
Collationstring// Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query
49+
Loc*time.Location// Location for time.Time values
50+
MaxAllowedPacketint// Max packet size allowed
51+
ServerPubKeystring// Server public key name
52+
TLSConfigstring// TLS configuration name
53+
TLS*tls.Config// TLS configuration, its priority is higher than TLSConfig
54+
Timeout time.Duration// Dial timeout
55+
ReadTimeout time.Duration// I/O read timeout
56+
WriteTimeout time.Duration// I/O write timeout
57+
LoggerLogger// Logger
58+
DialFuncfunc(ctx context.Context, network, addrstring) (net.Conn, error) // Specifies the dial function for creating connections
5859

5960
// boolean fields
6061

0 commit comments

Comments
(0)