File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -80,7 +80,16 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
80
80
dialsLock .RLock ()
81
81
dial , ok := dials [mc .cfg .Net ]
82
82
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 {
84
93
dctx := ctx
85
94
if mc .cfg .Timeout > 0 {
86
95
var cancel context.CancelFunc
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ package mysql
10
10
11
11
import (
12
12
"bytes"
13
+ "context"
13
14
"crypto/rsa"
14
15
"crypto/tls"
15
16
"errors"
@@ -65,6 +66,15 @@ type Config struct {
65
66
MultiStatements bool // Allow multiple statements in one query
66
67
ParseTime bool // Parse time values to time.Time
67
68
RejectReadOnly bool // Reject read-only connections
69
+
70
+ // DialFunc specifies the dial function for creating connections.
71
+ // If DialFunc is nil, the connector will attempt to find a dial function from the global registry (registered with RegisterDialContext).
72
+ // If no dial function is found even after checking the global registry, the net.Dialer will be used as a fallback.
73
+ //
74
+ // The dial function is responsible for establishing connections. By providing a custom dial function,
75
+ // users can flexibly control the process of connection establishment. Custom dial functions can be registered in the global registry
76
+ // to tailor connection behavior according to specific requirements.
77
+ DialFunc func (ctx context.Context , network , addr string ) (net.Conn , error )
68
78
}
69
79
70
80
// NewConfig creates a new Config and sets default values.
You can’t perform that action at this time.
0 commit comments