-
Notifications
You must be signed in to change notification settings - Fork 2.3k
packets: implemented compression protocol #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
e6c682c
packets: implemented compression protocol
77f6792
packets: implemented compression protocol CR changes
a0cf94b
packets: implemented compression protocol: remove bytes.Reset for bac…
4cdff28
Merge branch 'master' of https://github.com/go-sql-driver/mysql
d0ea1a4
reading working
477c9f8
writerly changes
996ed2d
PR 649: adding compression (second code review)
f74faed
do not query max_allowed_packet by default (#680)
julienschmidt b3a093e
packets: do not call function on nulled value (#678)
julienschmidt 5eaa5ff
ColumnType interfaces (#667)
julienschmidt ee46028
Add Aurora errno to rejectReadOnly check (#634)
jeffcharles 93aed73
allow successful TravisCI runs in forks (#639)
jmhodges 4f10ee5
Drop support for Go 1.6 and lower (#696)
julienschmidt 59b0f90
Make gofmt happy (#704)
julienschmidt 3fbf53a
Added support for custom string types in ConvertValue. (#623)
dsmontoya f9c6a2c
Implement NamedValueChecker for mysqlConn (#690)
pushrax 6046bf0
Fix Valuers by returning driver.ErrSkip if couldn't convert type inte…
randomjunk 385673a
statement: Fix conversion of Valuer (#710)
linxGnu 9031984
Fixed imports for appengine/cloudsql (#700)
rrbrussell 6992fad
Fix tls=true didn't work with host without port (#718)
methane 386f84b
Differentiate between BINARY and CHAR (#724)
kwoodhouse93 f853432
Test with latest Go patch versions (#693)
AlekSi d1a8b86
Fix prepared statement (#734)
methane 3167920
driver.ErrBadConn when init packet read fails (#736)
fb33a2c
packets: implemented compression protocol
f174605
packets: implemented compression protocol CR changes
dbd1e2b
third code review changes
3e12e32
PR 649: minor cleanup
17a06f1
Merge branch 'master' into master
methane 60bdaec
Sort AUTHORS
methane 422ab6f
Update dsn.go
methane 26ea544
cr4 changes
f339392
Merge branch 'master' into cr4
3e559a8
saving work with SimpleReader present
6ceaef6
removed buf from mysqlConn
97afd8d
Merge pull request #1 from bLamarche413/cr4
f617170
removed comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
package mysql | ||
|
||
import ( | ||
"bytes" | ||
"compress/zlib" | ||
"io" | ||
) | ||
|
||
const ( | ||
minCompressLength = 50 | ||
) | ||
|
||
type compressedReader struct { | ||
buf packetReader | ||
bytesBuf []byte | ||
mc *mysqlConn | ||
zr io.ReadCloser | ||
} | ||
|
||
type compressedWriter struct { | ||
connWriter io.Writer | ||
mc *mysqlConn | ||
zw *zlib.Writer | ||
} | ||
|
||
func newCompressedReader(buf packetReader, mc *mysqlConn) *compressedReader { | ||
return &compressedReader{ | ||
buf: buf, | ||
bytesBuf: make([]byte, 0), | ||
mc: mc, | ||
} | ||
} | ||
|
||
func newCompressedWriter(connWriter io.Writer, mc *mysqlConn) *compressedWriter { | ||
return &compressedWriter{ | ||
connWriter: connWriter, | ||
mc: mc, | ||
zw: zlib.NewWriter(new(bytes.Buffer)), | ||
} | ||
} | ||
|
||
func (cr *compressedReader) readNext(need int) ([]byte, error) { | ||
for len(cr.bytesBuf) < need { | ||
err := cr.uncompressPacket() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
data := cr.bytesBuf[:need] | ||
cr.bytesBuf = cr.bytesBuf[need:] | ||
return data, nil | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (cr *compressedReader) reuseBuffer(length int) []byte { | ||
return cr.buf.reuseBuffer(length) | ||
} | ||
|
||
func (cr *compressedReader) uncompressPacket() error { | ||
header, err := cr.buf.readNext(7) // size of compressed header | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// compressed header structure | ||
comprLength := int(uint32(header[0]) | uint32(header[1])<<8 | uint32(header[2])<<16) | ||
uncompressedLength := int(uint32(header[4]) | uint32(header[5])<<8 | uint32(header[6])<<16) | ||
compressionSequence := uint8(header[3]) | ||
|
||
if compressionSequence != cr.mc.compressionSequence { | ||
return ErrPktSync | ||
} | ||
|
||
cr.mc.compressionSequence++ | ||
|
||
comprData, err := cr.buf.readNext(comprLength) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// if payload is uncompressed, its length will be specified as zero, and its | ||
// true length is contained in comprLength | ||
if uncompressedLength == 0 { | ||
cr.bytesBuf = append(cr.bytesBuf, comprData...) | ||
return nil | ||
} | ||
|
||
// write comprData to a bytes.buffer, then read it using zlib into data | ||
br := bytes.NewReader(comprData) | ||
|
||
if cr.zr == nil { | ||
cr.zr, err = zlib.NewReader(br) | ||
} else { | ||
err = cr.zr.(zlib.Resetter).Reset(br, nil) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer cr.zr.Close() | ||
|
||
// use existing capacity in bytesBuf if possible | ||
offset := len(cr.bytesBuf) | ||
if cap(cr.bytesBuf)-offset < uncompressedLength { | ||
old := cr.bytesBuf | ||
cr.bytesBuf = make([]byte, offset, offset+uncompressedLength) | ||
copy(cr.bytesBuf, old) | ||
} | ||
|
||
data := cr.bytesBuf[offset : offset+uncompressedLength] | ||
|
||
lenRead := 0 | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// http://grokbase.com/t/gg/golang-nuts/146y9ppn6b/go-nuts-stream-compression-with-compress-flate | ||
for lenRead < uncompressedLength { | ||
n, err := cr.zr.Read(data[lenRead:]) | ||
lenRead += n | ||
|
||
if err == io.EOF { | ||
if lenRead < uncompressedLength { | ||
return io.ErrUnexpectedEOF | ||
} | ||
break | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
cr.bytesBuf = append(cr.bytesBuf, data...) | ||
|
||
return nil | ||
} | ||
|
||
func (cw *compressedWriter) Write(data []byte) (int, error) { | ||
// when asked to write an empty packet, do nothing | ||
if len(data) == 0 { | ||
return 0, nil | ||
} | ||
|
||
totalBytes := len(data) | ||
length := len(data) - 4 | ||
maxPayloadLength := maxPacketSize - 4 | ||
blankHeader := make([]byte, 7) | ||
|
||
for length >= maxPayloadLength { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why both of maxPayload := maxPacketSize - 4
for len(data) > maxPayload {
...
data = data[maxPayload:]
} |
||
payload := data[:maxPayloadLength] | ||
payloadLen := len(payload) | ||
|
||
bytesBuf := &bytes.Buffer{} | ||
bytesBuf.Write(blankHeader) | ||
cw.zw.Reset(bytesBuf) | ||
_, err := cw.zw.Write(payload) | ||
if err != nil { | ||
return 0, err | ||
} | ||
cw.zw.Close() | ||
|
||
// if compression expands the payload, do not compress | ||
compressedPayload := bytesBuf.Bytes() | ||
if len(compressedPayload) > maxPayloadLength { | ||
compressedPayload = append(blankHeader, payload...) | ||
payloadLen = 0 | ||
} | ||
|
||
err = cw.writeToNetwork(compressedPayload, payloadLen) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
length -= maxPayloadLength | ||
data = data[maxPayloadLength:] | ||
} | ||
|
||
payloadLen := len(data) | ||
|
||
// do not attempt compression if packet is too small | ||
if payloadLen < minCompressLength { | ||
err := cw.writeToNetwork(append(blankHeader, data...), 0) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return totalBytes, nil | ||
} | ||
|
||
bytesBuf := &bytes.Buffer{} | ||
bytesBuf.Write(blankHeader) | ||
cw.zw.Reset(bytesBuf) | ||
_, err := cw.zw.Write(data) | ||
if err != nil { | ||
return 0, err | ||
} | ||
cw.zw.Close() | ||
|
||
compressedPayload := bytesBuf.Bytes() | ||
|
||
if len(compressedPayload) > len(data) { | ||
compressedPayload = append(blankHeader, data...) | ||
payloadLen = 0 | ||
} | ||
|
||
// add header and send over the wire | ||
err = cw.writeToNetwork(compressedPayload, payloadLen) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return totalBytes, nil | ||
} | ||
|
||
func (cw *compressedWriter) writeToNetwork(data []byte, uncomprLength int) error { | ||
comprLength := len(data) - 7 | ||
|
||
// compression header | ||
data[0] = byte(0xff & comprLength) | ||
data[1] = byte(0xff & (comprLength >> 8)) | ||
data[2] = byte(0xff & (comprLength >> 16)) | ||
|
||
data[3] = cw.mc.compressionSequence | ||
|
||
// this value is never greater than maxPayloadLength | ||
data[4] = byte(0xff & uncomprLength) | ||
data[5] = byte(0xff & (uncomprLength >> 8)) | ||
data[6] = byte(0xff & (uncomprLength >> 16)) | ||
|
||
if _, err := cw.connWriter.Write(data); err != nil { | ||
return err | ||
} | ||
|
||
cw.mc.compressionSequence++ | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it really need to introduce two extra buffers?
Would it make sense to combine the reader and writer (e.g. as a virtual buffer) and share some data, like the
mc
and the buffer?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has been a while since I've looked at my own code so I may be wrong about this, but between
compressedReader
andcompressedWriter
, it appears the only thing they are sharing is a reference tomc
? The reader uses a slice, but that slice needs to act as memory between reads so I don't think it would make sense to share it. Please let me know what you meant by this comment!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That answers my question. Currently we only have one buffer for each connection, which is shared between reads and writes.