-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Implement Connector and DriverContext interface #778
Implement Connector and DriverContext interface #778
Conversation
because Connector.Connect is built with Go1.10 or higher.
See #705 |
I did. but #705 is missing some features.
|
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.
-
To simplify the API, the
Connector
should be merged withConfig
: just implement theConnect
andDriver
methods directly on*Config
.OpenConnector
would become a one-line wrapper aroundParseDSN
. -
Rename
connector.go
toconnector_go1.10.go
,driver_go110.go
todriver_go1.10.go
,driver_go110_test.go
todriver_go1.10_test.go
because the Go toolchain has special support for this naming scheme. This makes the// +build go1.10
lines redundant, so remove them. (see also Rename go version dependent sources using idiomatic naming #822 that applies this to all existing files) -
You duplicated the content of
Driver.Open
method inConnect
. This open the way to maintainance nightmare (bug could be fixed in one place but forgotten in the other). A refactoring is necessary to remove this duplication. I propose to move the common bits as a private method ofConfig
.
Do not merge |
We use "squash and merge". So no need to rebase. |
driver_go110_test.go
Outdated
@@ -0,0 +1,17 @@ | |||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | |||
// | |||
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved. |
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.
For new code, use current year.
because the Go toolchain has special support for this naming scheme.
d0b4b43
to
877ae16
Compare
Will we? #822 (comment) // Open new Connection.
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
// the DSN string is formated
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
c, err := ParseDSN(dsn)
if err != nil {
return nil, err
}
return c.Connect(context.Background())
} This is more simple solution for duplicated code. |
The context package has been supported from Go 1.6, and Go 1.6 has been dropped. |
connector.go
Outdated
maxAllowedPacket: maxPacketSize, | ||
maxWriteSize: maxPacketSize - 1, | ||
closech: make(chan struct{}), | ||
cfg: c, |
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 *Config
from ParseDSN
. Here, each connection returned by Connect
shares a pointer to the one Config
. 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.
connector.go
Outdated
// 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 { |
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.
This looks new during Connect
. What does it do?
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.
It cancels opening new connection, if the context is canceled.
See #608 please.
Other methods which supports the context have this lines. (e.g.
Lines 84 to 87 in 2307b45
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
Is there anything I can do to help this along? |
The conflict appears to be this small change in |
I resolved the conflict. Check it, please. |
I don't like Config implements Connector interface. type Connector struct {
cfg *Config // immutable private copy.
}
func NewConnector(cfg *Config) *Connector {
copyCfg := *cfg
// deep copy some members
return &Connector{cfg: cfg}
}
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
// ...
} This allows future optimization for us. For example, we can use one dedicated killing connection, |
It sounds good. |
Since this is the real solution to ":" in username any chance we can get this into the driver? |
Description
Implemented the Connector interface and DriverContext interface in go 1.10.
fixes #671
#705 also implements Connector interface, while the context support is incomplete.
Checklist