From 9c487b7b24bcd0ae8de89448fac0e4c2467b817d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 18 May 2024 16:06:03 +0900 Subject: [PATCH] improve packets.go --- packets.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/packets.go b/packets.go index f6f9cca4f..e14dc95cf 100644 --- a/packets.go +++ b/packets.go @@ -117,6 +117,8 @@ func (mc *mysqlConn) writePacket(data []byte) error { // Write packet if mc.writeTimeout > 0 { if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil { + mc.log(err) + mc.cleanup() return err } } @@ -296,7 +298,7 @@ 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.cleanup() // Avoid repeated "busy buffer" errors. + mc.cleanup() return err } @@ -515,32 +517,33 @@ func (mc *okHandler) readResultOK() error { } // Result Set Header Packet -// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset +// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response.html func (mc *okHandler) readResultSetHeaderPacket() (int, error) { // handleOkPacket replaces both values; other cases leave the values unchanged. mc.result.affectedRows = append(mc.result.affectedRows, 0) mc.result.insertIds = append(mc.result.insertIds, 0) data, err := mc.conn().readPacket() - if err == nil { - switch data[0] { - - case iOK: - return 0, mc.handleOkPacket(data) + if err != nil { + return 0, err + } - case iERR: - return 0, mc.conn().handleErrorPacket(data) + switch data[0] { + case iOK: + return 0, mc.handleOkPacket(data) - case iLocalInFile: - return 0, mc.handleInFileRequest(string(data[1:])) - } + case iERR: + return 0, mc.conn().handleErrorPacket(data) - // column count - num, _, _ := readLengthEncodedInteger(data) - // ignore remaining data in the packet. see #1478. - return int(num), nil + case iLocalInFile: + return 0, mc.handleInFileRequest(string(data[1:])) } - return 0, err + + // column count + // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset.html + num, _, _ := readLengthEncodedInteger(data) + // ignore remaining data in the packet. see #1478. + return int(num), nil } // Error Packet