Skip to content

Commit 15462c1

Browse files
zjjmethane
zjj
authored andcommitted
Set timeout for custom dialer. (go-sql-driver#1035)
Fixes go-sql-driver#1034.
1 parent b4242ba commit 15462c1

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ James Harr <james.harr at gmail.com>
4444
Jeff Hodges <jeff at somethingsimilar.com>
4545
Jeffrey Charles <jeffreycharles at gmail.com>
4646
Jerome Meyer <jxmeyer at gmail.com>
47+
Jiajia Zhong <zhong2plus at gmail.com>
4748
Jian Zhen <zhenjl at gmail.com>
4849
Joshua Prunier <joshua.prunier at gmail.com>
4950
Julien Lefevre <julien.lefevr at gmail.com>

connector.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
3737
dial, ok := dials[mc.cfg.Net]
3838
dialsLock.RUnlock()
3939
if ok {
40-
mc.netConn, err = dial(ctx, mc.cfg.Addr)
40+
dctx := ctx
41+
if mc.cfg.Timeout > 0 {
42+
var cancel context.CancelFunc
43+
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
44+
defer cancel()
45+
}
46+
mc.netConn, err = dial(dctx, mc.cfg.Addr)
4147
} else {
4248
nd := net.Dialer{Timeout: mc.cfg.Timeout}
4349
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)

0 commit comments

Comments
 (0)