From 2f7015e5c48d361a7dd188c01ae95379c7b9f6f9 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Jun 2024 19:12:35 +0900 Subject: [PATCH 1/3] log: add "filename:line" prefix by ourself (#1589) go-sql-driver/mysql#1563 broke the filename:lineno prefix in the log message by introducing a helper function. This commit adds the "filename:line" prefix in the helper function instead of log.Lshortfile option to show correct filename:lineno. --- connection.go | 12 ++++++++++++ errors.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 7b8abeb0..a3dc09d2 100644 --- a/connection.go +++ b/connection.go @@ -13,8 +13,10 @@ import ( "database/sql" "database/sql/driver" "encoding/json" + "fmt" "io" "net" + "runtime" "strconv" "strings" "sync/atomic" @@ -47,6 +49,16 @@ type mysqlConn struct { // Helper function to call per-connection logger. func (mc *mysqlConn) log(v ...any) { + _, filename, lineno, ok := runtime.Caller(1) + if ok { + pos := strings.LastIndexByte(filename, '/') + if pos != -1 { + filename = filename[pos+1:] + } + prefix := fmt.Sprintf("%s:%d ", filename, lineno) + v = append([]any{prefix}, v...) + } + mc.cfg.Logger.Print(v...) } diff --git a/errors.go b/errors.go index a7ef8890..238e480f 100644 --- a/errors.go +++ b/errors.go @@ -37,7 +37,7 @@ var ( errBadConnNoWrite = errors.New("bad connection") ) -var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile)) +var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime)) // Logger is used to log critical error messages. type Logger interface { From 05325d8c2d8a3f5469086f2fd15552cc7960926c Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Jun 2024 22:34:45 +0900 Subject: [PATCH 2/3] fix some write error handling (#1595) interpolateParams() returned ErrInvalidConn without closing the connection. Since database/sql doesn't understand ErrInvalidConn, there is a risk that database/sql reuse this connection and ErrInvalidConn is returned repeatedly. --- connection.go | 6 ++++-- packets.go | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index a3dc09d2..bf102cdf 100644 --- a/connection.go +++ b/connection.go @@ -230,8 +230,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin buf, err := mc.buf.takeCompleteBuffer() if err != nil { // can not take the buffer. Something must be wrong with the connection - mc.log(err) - return "", ErrInvalidConn + mc.cleanup() + // interpolateParams would be called before sending any query. + // So its safe to retry. + return "", driver.ErrBadConn } buf = buf[:0] argPos := 0 diff --git a/packets.go b/packets.go index cf3412ff..033ef201 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.cleanup() + mc.log(err) return err } } From 9b8d28eff68e1b0dec9d45e9868796e7f7a9af49 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Jun 2024 22:49:22 +0900 Subject: [PATCH 3/3] fix missing skip test when no DB is available (#1594) Fix `go test` fails when no DB is set up. --- driver_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/driver_test.go b/driver_test.go index 4fd196d4..24d73c34 100644 --- a/driver_test.go +++ b/driver_test.go @@ -3539,6 +3539,9 @@ func TestConnectionAttributes(t *testing.T) { } func TestErrorInMultiResult(t *testing.T) { + if !available { + t.Skipf("MySQL server not running on %s", netAddr) + } // https://github.com/go-sql-driver/mysql/issues/1361 var db *sql.DB if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation {