Skip to content

Commit ac7765c

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

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

connector.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
8080
dialsLock.RLock()
8181
dial, ok := dials[mc.cfg.Net]
8282
dialsLock.RUnlock()
83-
if ok {
83+
84+
if c.cfg.DialFunc != nil {
85+
dctx := ctx
86+
if mc.cfg.Timeout > 0 {
87+
var cancel context.CancelFunc
88+
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
89+
defer cancel()
90+
}
91+
mc.netConn, err = c.cfg.DialFunc(dctx, mc.cfg.Net, mc.cfg.Addr)
92+
} else if ok {
8493
dctx := ctx
8594
if mc.cfg.Timeout > 0 {
8695
var cancel context.CancelFunc

dsn.go

+4
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"
@@ -65,6 +66,9 @@ type Config struct {
6566
MultiStatements bool // Allow multiple statements in one query
6667
ParseTime bool // Parse time values to time.Time
6768
RejectReadOnly bool // Reject read-only connections
69+
70+
// Specify a custom dial function per config instead of using RegisterDialContext
71+
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
6872
}
6973

7074
// NewConfig creates a new Config and sets default values.

0 commit comments

Comments
 (0)