-
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
remove errBadConnNoWrite and markBadConn #1583
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -111,15 +111,6 @@ func (mc *mysqlConn) handleParams() (err error) { | |
return | ||
} | ||
|
||
// markBadConn replaces errBadConnNoWrite with driver.ErrBadConn. | ||
// This function is used to return driver.ErrBadConn only when safe to retry. | ||
func (mc *mysqlConn) markBadConn(err error) error { | ||
if err == errBadConnNoWrite { | ||
return driver.ErrBadConn | ||
} | ||
return err | ||
} | ||
|
||
func (mc *mysqlConn) Begin() (driver.Tx, error) { | ||
return mc.begin(false) | ||
} | ||
|
@@ -138,7 +129,7 @@ func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) { | |
if err == nil { | ||
return &mysqlTx{mc}, err | ||
} | ||
return nil, mc.markBadConn(err) | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper error handling and propagation. The error handling in the |
||
} | ||
|
||
func (mc *mysqlConn) Close() (err error) { | ||
|
@@ -340,15 +331,15 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err | |
copied := mc.result | ||
return &copied, err | ||
} | ||
return nil, mc.markBadConn(err) | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review the error handling in the The error handling in the |
||
} | ||
|
||
// Internal function to execute commands | ||
func (mc *mysqlConn) exec(query string) error { | ||
handleOk := mc.clearResult() | ||
// Send command | ||
if err := mc.writeCommandPacketStr(comQuery, query); err != nil { | ||
return mc.markBadConn(err) | ||
return err | ||
} | ||
|
||
// Read Result | ||
|
@@ -378,10 +369,10 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro | |
|
||
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) { | ||
handleOk := mc.clearResult() | ||
|
||
if mc.closed.Load() { | ||
return nil, driver.ErrBadConn | ||
} | ||
|
||
if len(args) != 0 { | ||
if !mc.cfg.InterpolateParams { | ||
return nil, driver.ErrSkip | ||
|
@@ -393,10 +384,11 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) | |
} | ||
query = prepared | ||
} | ||
|
||
// Send command | ||
err := mc.writeCommandPacketStr(comQuery, query) | ||
if err != nil { | ||
return nil, mc.markBadConn(err) | ||
return nil, err | ||
} | ||
|
||
// Read Result | ||
|
@@ -487,7 +479,7 @@ func (mc *mysqlConn) Ping(ctx context.Context) (err error) { | |
|
||
handleOk := mc.clearResult() | ||
if err = mc.writeCommandPacket(comPing); err != nil { | ||
return mc.markBadConn(err) | ||
return err | ||
} | ||
|
||
return handleOk.readResultOK() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,17 +125,11 @@ func (mc *mysqlConn) writePacket(data []byte) error { | |
|
||
n, err := mc.netConn.Write(data[:4+size]) | ||
if err != nil { | ||
mc.cleanup() | ||
if cerr := mc.canceled.Value(); cerr != nil { | ||
return cerr | ||
} | ||
mc.cleanup() | ||
if n == 0 && pktLen == len(data)-4 { | ||
// only for the first loop iteration when nothing was written yet | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
} else { | ||
return err | ||
} | ||
return err | ||
} | ||
if n != 4+size { | ||
// io.Writer(b) must return a non-nil error if it cannot write len(b) bytes. | ||
|
@@ -305,8 +299,8 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string | |
data, err := mc.buf.takeBuffer(pktLen + 4) | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
Comment on lines
+302
to
+303
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor repeated error handling patterns. There is a repeated pattern where a buffer acquisition fails and - mc.cleanup()
- return err
+ return handleBufferAcquisitionError(mc, err) And then define the helper function: func handleBufferAcquisitionError(mc *mysqlConn, err error) error {
mc.cleanup()
return err
} Also applies to: 391-392, 411-412, 430-431, 451-452 |
||
} | ||
|
||
// ClientFlags [32 bit] | ||
|
@@ -394,8 +388,8 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte) error { | |
data, err := mc.buf.takeSmallBuffer(pktLen) | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
} | ||
|
||
// Add the auth data [EOF] | ||
|
@@ -414,8 +408,8 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error { | |
data, err := mc.buf.takeSmallBuffer(4 + 1) | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
} | ||
|
||
// Add command byte | ||
|
@@ -433,8 +427,8 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error { | |
data, err := mc.buf.takeBuffer(pktLen + 4) | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
} | ||
|
||
// Add command byte | ||
|
@@ -454,8 +448,8 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error { | |
data, err := mc.buf.takeSmallBuffer(4 + 1 + 4) | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
} | ||
|
||
// Add command byte | ||
|
@@ -997,8 +991,8 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error { | |
} | ||
if err != nil { | ||
// cannot take the buffer. Something must be wrong with the connection | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
Comment on lines
+994
to
+995
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle buffer acquisition failures more gracefully. When buffer acquisition fails, the method |
||
} | ||
|
||
// command [1 byte] | ||
|
@@ -1196,8 +1190,8 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error { | |
if valuesCap != cap(paramValues) { | ||
data = append(data[:pos], paramValues...) | ||
if err = mc.buf.store(data); err != nil { | ||
mc.log(err) | ||
return errBadConnNoWrite | ||
mc.cleanup() | ||
return err | ||
} | ||
} | ||
|
||
|
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 function and errBadConnNoWrite were completely meaningless. There is no need to use errBadConnNoWrite once and convert it to ErrBadConn, as you can just use ErrBadConn directly if you are not sending anything yet.
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.
I changed my mind.
markBadConn()
is allowlist for ErrBadConn.If writePacket() returned driver.ErrBadConn instead of errBadConnNoWrite, all error check need to suppress ErrBadConn.
So until we stop returning ErrBadConn, markBadConn() should alive.