Skip to content

Commit 3e46420

Browse files
author
Reed Allman
committed
driver.ErrBadConn when init packet read fails
when initializing a connection, we can return drivers.ErrBadConn so that the sql package can use this information to retry getting a connection. the sql package will not give us this behavior unless we return that specific error. pursuant to #302, this operation is another in the limited set of things that seems safe to retry. we are trying to move to the post-302 world and ran into this :) I've run into this when using the new drivers.Conn() interface specifically, though after digging into the sql package this seems like a similar path for any query. After changing this return, I end up eventually getting a new conn that is valid. It appears in my repro like the db was not yet ready, and would just end up returning an invalid conn and giving up, the InvalidConn err came from https://github.com/go-sql-driver/mysql/blob/4a0c3d73d8579f9fc535cf5e654a651cbd57dd6e/packets.go#L38 which was the first read on the packet (an EOF). At least, returning a drivers.ErrBadConn to let the db retry initializing the connection seems like a safe operation. thanks for maintaining this library :)
1 parent 4a0c3d7 commit 3e46420

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

packets.go

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ func (mc *mysqlConn) writePacket(data []byte) error {
157157
func (mc *mysqlConn) readInitPacket() ([]byte, error) {
158158
data, err := mc.readPacket()
159159
if err != nil {
160+
// for init we can rewrite this to ErrBadConn for sql.Driver to retry, since
161+
// in connection initialization we don't risk retrying non-idempotent actions.
162+
if err == ErrInvalidConn {
163+
return nil, driver.ErrBadConn
164+
}
160165
return nil, err
161166
}
162167

0 commit comments

Comments
 (0)