Skip to content

Commit

Permalink
Set timeout for custom dialer. (go-sql-driver#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjj authored and tz70s committed Sep 5, 2020
1 parent 0d5df0a commit 6a5285e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ James Harr <james.harr at gmail.com>
Jeff Hodges <jeff at somethingsimilar.com>
Jeffrey Charles <jeffreycharles at gmail.com>
Jerome Meyer <jxmeyer at gmail.com>
Jiajia Zhong <zhong2plus at gmail.com>
Jian Zhen <zhenjl at gmail.com>
Joshua Prunier <joshua.prunier at gmail.com>
Julien Lefevre <julien.lefevr at gmail.com>
Expand Down
8 changes: 7 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
dial, ok := dials[mc.cfg.Net]
dialsLock.RUnlock()
if ok {
mc.netConn, err = dial(ctx, mc.cfg.Addr)
dctx := ctx
if mc.cfg.Timeout > 0 {
var cancel context.CancelFunc
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
defer cancel()
}
mc.netConn, err = dial(dctx, mc.cfg.Addr)
} else {
nd := net.Dialer{Timeout: mc.cfg.Timeout}
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
Expand Down

0 comments on commit 6a5285e

Please sign in to comment.