From ac7765cedabe4a1f47eaba6fc5fd330fa704a8bc Mon Sep 17 00:00:00 2001 From: Aaron Jheng Date: Fri, 15 Dec 2023 07:36:52 +0000 Subject: [PATCH] Specify a custom dial function per config --- connector.go | 11 ++++++++++- dsn.go | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/connector.go b/connector.go index 3cef7963f..29b6dc1b7 100644 --- a/connector.go +++ b/connector.go @@ -80,7 +80,16 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { dialsLock.RLock() dial, ok := dials[mc.cfg.Net] dialsLock.RUnlock() - if ok { + + if c.cfg.DialFunc != nil { + dctx := ctx + if mc.cfg.Timeout > 0 { + var cancel context.CancelFunc + dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout) + defer cancel() + } + mc.netConn, err = c.cfg.DialFunc(dctx, mc.cfg.Net, mc.cfg.Addr) + } else if ok { dctx := ctx if mc.cfg.Timeout > 0 { var cancel context.CancelFunc diff --git a/dsn.go b/dsn.go index ef0608636..bb8f35ee3 100644 --- a/dsn.go +++ b/dsn.go @@ -10,6 +10,7 @@ package mysql import ( "bytes" + "context" "crypto/rsa" "crypto/tls" "errors" @@ -65,6 +66,9 @@ type Config struct { MultiStatements bool // Allow multiple statements in one query ParseTime bool // Parse time values to time.Time RejectReadOnly bool // Reject read-only connections + + // Specify a custom dial function per config instead of using RegisterDialContext + DialFunc func(ctx context.Context, network, addr string) (net.Conn, error) } // NewConfig creates a new Config and sets default values.