Skip to content

Commit 0e3ace3

Browse files
committed
code cleanup
1 parent 39e52e4 commit 0e3ace3

File tree

5 files changed

+64
-50
lines changed

5 files changed

+64
-50
lines changed

compress.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2024 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
19
package mysql
210

311
import (
@@ -120,10 +128,10 @@ func (c *compressor) uncompressPacket() error {
120128
fmt.Fprintf(os.Stderr, "uncompress cmplen=%v uncomplen=%v seq=%v\n",
121129
comprLength, uncompressedLength, compressionSequence)
122130
}
123-
if compressionSequence != c.mc.compressionSequence {
131+
if compressionSequence != c.mc.compresSequence {
124132
return ErrPktSync
125133
}
126-
c.mc.compressionSequence++
134+
c.mc.compresSequence++
127135

128136
comprData, err := c.mc.buf.readNext(comprLength)
129137
if err != nil {
@@ -206,15 +214,15 @@ func (c *compressor) writeCompressedPacket(data []byte, uncompressedLen int) err
206214
c.mc.cfg.Logger.Print(
207215
fmt.Sprintf(
208216
"writeCompressedPacket: comprLength=%v, uncompressedLen=%v, seq=%v",
209-
comprLength, uncompressedLen, c.mc.compressionSequence))
217+
comprLength, uncompressedLen, c.mc.compresSequence))
210218
}
211219

212220
// compression header
213221
data[0] = byte(0xff & comprLength)
214222
data[1] = byte(0xff & (comprLength >> 8))
215223
data[2] = byte(0xff & (comprLength >> 16))
216224

217-
data[3] = c.mc.compressionSequence
225+
data[3] = c.mc.compresSequence
218226

219227
// this value is never greater than maxPayloadLength
220228
data[4] = byte(0xff & uncompressedLen)
@@ -226,6 +234,6 @@ func (c *compressor) writeCompressedPacket(data []byte, uncompressedLen int) err
226234
return err
227235
}
228236

229-
c.mc.compressionSequence++
237+
c.mc.compresSequence++
230238
return nil
231239
}

compress_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2024 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
19
package mysql
210

311
import (
@@ -30,7 +38,7 @@ func newMockBuf(data []byte) buffer {
3038
func compressHelper(t *testing.T, mc *mysqlConn, uncompressedPacket []byte) []byte {
3139
// get status variables
3240

33-
cs := mc.compressionSequence
41+
cs := mc.compresSequence
3442

3543
var b bytes.Buffer
3644
cw := newCompressor(mc, &b)
@@ -46,13 +54,13 @@ func compressHelper(t *testing.T, mc *mysqlConn, uncompressedPacket []byte) []by
4654
}
4755

4856
if len(uncompressedPacket) > 0 {
49-
if mc.compressionSequence != (cs + 1) {
50-
t.Fatalf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compressionSequence)
57+
if mc.compresSequence != (cs + 1) {
58+
t.Fatalf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compresSequence)
5159
}
5260

5361
} else {
54-
if mc.compressionSequence != cs {
55-
t.Fatalf("mc.compressionSequence updated incorrectly for case of empty write, expected %d and saw %d", cs, mc.compressionSequence)
62+
if mc.compresSequence != cs {
63+
t.Fatalf("mc.compressionSequence updated incorrectly for case of empty write, expected %d and saw %d", cs, mc.compresSequence)
5664
}
5765
}
5866

@@ -62,7 +70,7 @@ func compressHelper(t *testing.T, mc *mysqlConn, uncompressedPacket []byte) []by
6270
// uncompressHelper uncompresses compressedPacket and checks state variables
6371
func uncompressHelper(t *testing.T, mc *mysqlConn, compressedPacket []byte, expSize int) []byte {
6472
// get status variables
65-
cs := mc.compressionSequence
73+
cs := mc.compresSequence
6674

6775
// mocking out buf variable
6876
mc.buf = newMockBuf(compressedPacket)
@@ -76,12 +84,12 @@ func uncompressHelper(t *testing.T, mc *mysqlConn, compressedPacket []byte, expS
7684
}
7785

7886
if expSize > 0 {
79-
if mc.compressionSequence != (cs + 1) {
80-
t.Fatalf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compressionSequence)
87+
if mc.compresSequence != (cs + 1) {
88+
t.Fatalf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compresSequence)
8189
}
8290
} else {
83-
if mc.compressionSequence != cs {
84-
t.Fatalf("mc.compressionSequence updated incorrectly for case of empty read, expected %d and saw %d", cs, mc.compressionSequence)
91+
if mc.compresSequence != cs {
92+
t.Fatalf("mc.compressionSequence updated incorrectly for case of empty read, expected %d and saw %d", cs, mc.compresSequence)
8593
}
8694
}
8795
return uncompressedPacket

connection.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import (
2121
)
2222

2323
type mysqlConn struct {
24-
buf buffer
25-
netConn net.Conn
26-
rawConn net.Conn // underlying connection when netConn is TLS connection.
27-
result mysqlResult // managed by clearResult() and handleOkPacket().
28-
packetReader packetReader
29-
packetWriter io.Writer
30-
cfg *Config
31-
connector *connector
32-
maxAllowedPacket int
33-
maxWriteSize int
34-
writeTimeout time.Duration
35-
flags clientFlag
36-
status statusFlag
37-
sequence uint8
38-
compressionSequence uint8
39-
parseTime bool
40-
compress bool
24+
buf buffer
25+
netConn net.Conn
26+
rawConn net.Conn // underlying connection when netConn is TLS connection.
27+
result mysqlResult // managed by clearResult() and handleOkPacket().
28+
packetReader packetReader
29+
packetWriter io.Writer
30+
cfg *Config
31+
connector *connector
32+
maxAllowedPacket int
33+
maxWriteSize int
34+
writeTimeout time.Duration
35+
flags clientFlag
36+
status statusFlag
37+
sequence uint8
38+
compresSequence uint8
39+
parseTime bool
40+
compress bool
4141

4242
// for context support (Go 1.8+)
4343
watching bool
@@ -52,21 +52,19 @@ type packetReader interface {
5252
readNext(need int) ([]byte, error)
5353
}
5454

55-
func (mc *mysqlConn) resetSeqNo() {
55+
func (mc *mysqlConn) resetSequenceNr() {
5656
mc.sequence = 0
57-
mc.compressionSequence = 0
57+
mc.compresSequence = 0
5858
}
5959

60-
// syncSeqNo must be called when:
61-
// - at least one large packet is sent (split packet happend), and
62-
// - finished writing, before start reading.
63-
func (mc *mysqlConn) syncSeqNo() {
64-
// This syncs compressionSequence to sequence.
65-
// This is done in `net_flush()` in MySQL and MariaDB.
60+
// syncSequenceNr must be called when finished writing some packet and before start reading.
61+
func (mc *mysqlConn) syncSequenceNr() {
62+
// Syncs compressionSequence to sequence.
63+
// This is not documented but done in `net_flush()` in MySQL and MariaDB.
6664
// https://github.com/mariadb-corporation/mariadb-connector-c/blob/8228164f850b12353da24df1b93a1e53cc5e85e9/libmariadb/ma_net.c#L170-L171
6765
// https://github.com/mysql/mysql-server/blob/824e2b4064053f7daf17d7f3f84b7a3ed92e5fb4/sql-common/net_serv.cc#L293
6866
if mc.compress {
69-
mc.sequence = mc.compressionSequence
67+
mc.sequence = mc.compresSequence
7068
}
7169
}
7270

infile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (mc *okHandler) handleInFileRequest(name string) (err error) {
171171
if ioErr := mc.conn().writePacket(data[:4]); ioErr != nil {
172172
return ioErr
173173
}
174-
mc.conn().syncSeqNo()
174+
mc.conn().syncSequenceNr()
175175

176176
// read OK packet
177177
if err == nil {

packets.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte) error {
422422

423423
func (mc *mysqlConn) writeCommandPacket(command byte) error {
424424
// Reset Packet Sequence
425-
mc.resetSeqNo()
425+
mc.resetSequenceNr()
426426

427427
data, err := mc.buf.takeSmallBuffer(4 + 1)
428428
if err != nil {
@@ -440,7 +440,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
440440

441441
func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
442442
// Reset Packet Sequence
443-
mc.resetSeqNo()
443+
mc.resetSequenceNr()
444444

445445
pktLen := 1 + len(arg)
446446
data, err := mc.buf.takeBuffer(pktLen + 4)
@@ -458,13 +458,13 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
458458

459459
// Send CMD packet
460460
err = mc.writePacket(data)
461-
mc.syncSeqNo()
461+
mc.syncSequenceNr()
462462
return err
463463
}
464464

465465
func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
466466
// Reset Packet Sequence
467-
mc.resetSeqNo()
467+
mc.resetSequenceNr()
468468

469469
data, err := mc.buf.takeSmallBuffer(4 + 1 + 4)
470470
if err != nil {
@@ -948,7 +948,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
948948
pktLen = dataOffset + argLen
949949
}
950950

951-
stmt.mc.resetSeqNo()
951+
stmt.mc.resetSequenceNr()
952952
// Add command byte [1 byte]
953953
data[4] = comStmtSendLongData
954954

@@ -972,7 +972,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
972972
}
973973

974974
// Reset Packet Sequence
975-
stmt.mc.resetSeqNo()
975+
stmt.mc.resetSequenceNr()
976976
return nil
977977
}
978978

@@ -997,7 +997,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
997997
}
998998

999999
// Reset packet-sequence
1000-
mc.resetSeqNo()
1000+
mc.resetSequenceNr()
10011001

10021002
var data []byte
10031003
var err error
@@ -1219,7 +1219,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
12191219
}
12201220

12211221
err = mc.writePacket(data)
1222-
mc.syncSeqNo()
1222+
mc.syncSequenceNr()
12231223
return err
12241224
}
12251225

0 commit comments

Comments
 (0)