Skip to content

Commit

Permalink
Merge branch 'master' into bye-errBadConnNoWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
methane authored Jun 11, 2024
2 parents f58f76c + 9b8d28e commit 30b95e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"net"
"runtime"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -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...)
}

Expand Down Expand Up @@ -209,7 +221,6 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
mc.cleanup()
// interpolateParams would be called before sending any query.
// So its safe to retry.
mc.log(err)
return "", driver.ErrBadConn
}
buf = buf[:0]
Expand Down
3 changes: 3 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
ErrBusyBuffer = errors.New("busy buffer")
)

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 {
Expand Down
2 changes: 1 addition & 1 deletion packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +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()
mc.log(err)
return err
}
}
Expand Down

0 comments on commit 30b95e8

Please sign in to comment.