-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement Connector and DriverContext interface #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
ca1671a
ccd2622
cc53a93
f1a63a4
877ae16
0aca26b
cfaf043
a1d847f
49e2480
82898e3
ee66ae6
069b529
0891474
d081e4e
8982810
40d78d5
1b00bed
255f988
af9889e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||||||
| // Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||||||||||
| // | ||||||||||
| // Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||||||||||
| // | ||||||||||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||||||||||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||||||||||
| // You can obtain one at http://mozilla.org/MPL/2.0/. | ||||||||||
|
|
||||||||||
| package mysql | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "context" | ||||||||||
| "database/sql/driver" | ||||||||||
| "net" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // Connect implements driver.Connector interface. | ||||||||||
| // Connect returns a connection to the database. | ||||||||||
| func (c *Config) Connect(ctx context.Context) (driver.Conn, error) { | ||||||||||
| var err error | ||||||||||
|
|
||||||||||
| // New mysqlConn | ||||||||||
| mc := &mysqlConn{ | ||||||||||
| maxAllowedPacket: maxPacketSize, | ||||||||||
| maxWriteSize: maxPacketSize - 1, | ||||||||||
| closech: make(chan struct{}), | ||||||||||
| cfg: c, | ||||||||||
| } | ||||||||||
| mc.parseTime = mc.cfg.ParseTime | ||||||||||
|
|
||||||||||
| // Connect to Server | ||||||||||
| // TODO: needs RegisterDialContext | ||||||||||
| dialsLock.RLock() | ||||||||||
| dial, ok := dials[mc.cfg.Net] | ||||||||||
| dialsLock.RUnlock() | ||||||||||
| if ok { | ||||||||||
| mc.netConn, err = dial(mc.cfg.Addr) | ||||||||||
| } else { | ||||||||||
| nd := net.Dialer{Timeout: mc.cfg.Timeout} | ||||||||||
| mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr) | ||||||||||
| } | ||||||||||
| if err != nil { | ||||||||||
| return nil, err | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Enable TCP Keepalives on TCP connections | ||||||||||
| if tc, ok := mc.netConn.(*net.TCPConn); ok { | ||||||||||
| if err := tc.SetKeepAlive(true); err != nil { | ||||||||||
| // Don't send COM_QUIT before handshake. | ||||||||||
| mc.netConn.Close() | ||||||||||
| mc.netConn = nil | ||||||||||
| return nil, err | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Call startWatcher for context support (From Go 1.8) | ||||||||||
| if s, ok := interface{}(mc).(watcher); ok { | ||||||||||
| s.startWatcher() | ||||||||||
| if err := s.watchCancel(ctx); err != nil { | ||||||||||
|
||||||||||
| if err := mc.watchCancel(ctx); err != nil { | |
| return nil, err | |
| } | |
| defer mc.finish() |
I did not need it because
MySQLDriver.Open did not correspond to the context so far, but since Config.Connect become to correspond to the context, I added this
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
| // | ||
| // Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| // +build go1.10 | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| package mysql | ||
|
|
||
| import ( | ||
| "database/sql/driver" | ||
| ) | ||
|
|
||
| // OpenConnector implements driver.DriverContext. | ||
| func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) { | ||
| return ParseDSN(dsn) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
| // | ||
| // Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| // +build go1.10 | ||
|
|
||
| package mysql | ||
|
|
||
| import ( | ||
| "database/sql/driver" | ||
| ) | ||
|
|
||
| var _ driver.DriverContext = &MySQLDriver{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this change, a connection would get its own
*ConfigfromParseDSN. Here, each connection returned byConnectshares a pointer to the oneConfig. Is that intentional/desirable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally all connections would share one immutable copy.
One goal of the connector interface was to avoid having to parse and allocate a new config for each connection.