@@ -25,21 +25,11 @@ const (
2525 CompressionZstd
2626)
2727
28- type rwStatus int
29-
30- const (
31- rwNone rwStatus = iota
32- rwRead
33- rwWrite
34- )
35-
3628const (
3729 // maxCompressedSize is the max uncompressed data size for a compressed packet.
3830 // Packets bigger than maxCompressedSize will be split into multiple compressed packets.
3931 // MySQL has 16K for the first packet. The rest packets and MySQL Connector/J are 16M.
40- // Two restrictions for the length:
41- // - it should be smaller than 16M so that the length can fit in the 3 byte field in the header.
42- // - it should be larger than 4M so that the compressed sequence can fit in the 3 byte field when max_allowed_packet is 1G.
32+ // It should be smaller than 16M so that the length can fit in the 3 byte field in the header.
4333 maxCompressedSize = 1 << 24 - 1
4434 // minCompressSize is the min uncompressed data size for compressed data.
4535 // Packets smaller than minCompressSize won't be compressed.
@@ -83,26 +73,25 @@ func newCompressedReadWriter(rw packetReadWriter, algorithm CompressAlgorithm, z
8373 }
8474}
8575
86- func (crw * compressedReadWriter ) SetSequence ( sequence uint8 ) {
87- crw .packetReadWriter .SetSequence ( sequence )
76+ func (crw * compressedReadWriter ) ResetSequence ( ) {
77+ crw .packetReadWriter .ResetSequence ( )
8878 // Reset the compressed sequence before the next command.
89- if sequence == 0 {
90- crw .sequence = 0
91- crw .rwStatus = rwNone
92- }
79+ // Sequence wraps around once it hits 0xFF, so we need ResetSequence() to know that it's reset instead of overflow.
80+ crw .sequence = 0
81+ crw .rwStatus = rwNone
9382}
9483
84+ // BeginRW implements packetReadWriter.BeginRW.
9585// Uncompressed sequence of MySQL doesn't follow the spec: it's set to the compressed sequence when
9686// the client/server begins reading or writing.
97- func (crw * compressedReadWriter ) beginRW (status rwStatus ) {
87+ func (crw * compressedReadWriter ) BeginRW (status rwStatus ) {
9888 if crw .rwStatus != status {
9989 crw .packetReadWriter .SetSequence (crw .sequence )
10090 crw .rwStatus = status
10191 }
10292}
10393
10494func (crw * compressedReadWriter ) Read (p []byte ) (n int , err error ) {
105- crw .beginRW (rwRead )
10695 // Read from the connection to fill the buffer if the buffer is empty.
10796 if crw .readBuffer .Len () == 0 {
10897 if err = crw .readFromConn (); err != nil {
@@ -156,7 +145,6 @@ func (crw *compressedReadWriter) readFromConn() error {
156145}
157146
158147func (crw * compressedReadWriter ) Write (data []byte ) (n int , err error ) {
159- crw .beginRW (rwWrite )
160148 for {
161149 remainingLen := maxCompressedSize - crw .writeBuffer .Len ()
162150 if len (data ) <= remainingLen {
@@ -234,7 +222,6 @@ func (crw *compressedReadWriter) DirectWrite(data []byte) (n int, err error) {
234222// Peek won't be used.
235223// Notice: the peeked data may be discarded if an error is returned.
236224func (crw * compressedReadWriter ) Peek (n int ) (data []byte , err error ) {
237- crw .beginRW (rwRead )
238225 for crw .readBuffer .Len () < n {
239226 if err = crw .readFromConn (); err != nil {
240227 return
@@ -247,7 +234,6 @@ func (crw *compressedReadWriter) Peek(n int) (data []byte, err error) {
247234
248235// Discard won't be used.
249236func (crw * compressedReadWriter ) Discard (n int ) (d int , err error ) {
250- crw .beginRW (rwRead )
251237 for crw .readBuffer .Len () < n {
252238 if err = crw .readFromConn (); err != nil {
253239 return
0 commit comments