Skip to content

Commit 451e17f

Browse files
committed
Add support for length encoded connection attributes
1 parent c175348 commit 451e17f

File tree

5 files changed

+16
-31
lines changed

5 files changed

+16
-31
lines changed

connector.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package mysql
1111
import (
1212
"context"
1313
"database/sql/driver"
14-
"fmt"
1514
"net"
1615
"os"
1716
"strconv"
@@ -24,7 +23,7 @@ type connector struct {
2423
}
2524

2625
func encodeConnectionAttributes(textAttributes string) string {
27-
connAttrsBuf := make([]byte, 0, 251)
26+
connAttrsBuf := make([]byte, 0)
2827

2928
// default connection attributes
3029
connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrClientName)
@@ -49,15 +48,12 @@ func encodeConnectionAttributes(textAttributes string) string {
4948
return string(connAttrsBuf)
5049
}
5150

52-
func newConnector(cfg *Config) (*connector, error) {
51+
func newConnector(cfg *Config) *connector {
5352
encodedAttributes := encodeConnectionAttributes(cfg.ConnectionAttributes)
54-
if len(encodedAttributes) > 250 {
55-
return nil, fmt.Errorf("connection attributes are longer than 250 bytes: %dbytes (%q)", len(encodedAttributes), cfg.ConnectionAttributes)
56-
}
5753
return &connector{
5854
cfg: cfg,
5955
encodedAttributes: encodedAttributes,
60-
}, nil
56+
}
6157
}
6258

6359
// Connect implements driver.Connector interface.

connector_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ import (
88
)
99

1010
func TestConnectorReturnsTimeout(t *testing.T) {
11-
connector, err := newConnector(&Config{
11+
connector := newConnector(&Config{
1212
Net: "tcp",
1313
Addr: "1.1.1.1:1234",
1414
Timeout: 10 * time.Millisecond,
1515
})
16-
if err != nil {
17-
t.Fatal(err)
18-
}
1916

20-
_, err = connector.Connect(context.Background())
17+
_, err := connector.Connect(context.Background())
2118
if err == nil {
2219
t.Fatal("error expected")
2320
}

driver.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
8383
if err != nil {
8484
return nil, err
8585
}
86-
c, err := newConnector(cfg)
87-
if err != nil {
88-
return nil, err
89-
}
86+
c := newConnector(cfg)
9087
return c.Connect(context.Background())
9188
}
9289

@@ -108,7 +105,7 @@ func NewConnector(cfg *Config) (driver.Connector, error) {
108105
if err := cfg.normalize(); err != nil {
109106
return nil, err
110107
}
111-
return newConnector(cfg)
108+
return newConnector(cfg), nil
112109
}
113110

114111
// OpenConnector implements driver.DriverContext.
@@ -117,5 +114,5 @@ func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
117114
if err != nil {
118115
return nil, err
119116
}
120-
return newConnector(cfg)
117+
return newConnector(cfg), nil
121118
}

packets.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,14 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
292292
pktLen += n + 1
293293
}
294294

295-
// 1 byte to store length of all key-values
296-
// NOTE: Actually, this is length encoded integer.
297-
// But we support only len(connAttrBuf) < 251 for now because takeSmallBuffer
298-
// doesn't support buffer size more than 4096 bytes.
299-
// TODO(methane): Rewrite buffer management.
300-
pktLen += 1 + len(mc.connector.encodedAttributes)
295+
// encode length of the connection attributes
296+
var connAttrsLEIBuf [9]byte
297+
connAttrsLen := len(mc.connector.encodedAttributes)
298+
connAttrsLEI := appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen))
299+
pktLen += len(connAttrsLEI) + len(mc.connector.encodedAttributes)
301300

302301
// Calculate packet length and get buffer with that size
303-
data, err := mc.buf.takeSmallBuffer(pktLen + 4)
302+
data, err := mc.buf.takeBuffer(pktLen + 4)
304303
if err != nil {
305304
// cannot take the buffer. Something must be wrong with the connection
306305
mc.cfg.Logger.Print(err)
@@ -380,8 +379,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
380379
pos++
381380

382381
// Connection Attributes
383-
data[pos] = byte(len(mc.connector.encodedAttributes))
384-
pos++
382+
pos += copy(data[pos:], connAttrsLEI)
385383
pos += copy(data[pos:], []byte(mc.connector.encodedAttributes))
386384

387385
// Send Auth packet

packets_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ var _ net.Conn = new(mockConn)
9696

9797
func newRWMockConn(sequence uint8) (*mockConn, *mysqlConn) {
9898
conn := new(mockConn)
99-
connector, err := newConnector(NewConfig())
100-
if err != nil {
101-
panic(err)
102-
}
99+
connector := newConnector(NewConfig())
103100
mc := &mysqlConn{
104101
buf: newBuffer(conn),
105102
cfg: connector.cfg,

0 commit comments

Comments
 (0)