Skip to content

Commit 6ceaef6

Browse files
author
Brigitte Lamarche
committed
removed buf from mysqlConn
1 parent 3e559a8 commit 6ceaef6

9 files changed

+58
-90
lines changed

benchmark_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func BenchmarkRoundtripBin(b *testing.B) {
205205
length = max
206206
}
207207
test := sample[0:length]
208-
rows := tb.checkRows(stmt.Query(test))
208+
rows := tb.checkRows(stmt.Query(test)) //run benchmark tests to test that bit of code
209209
if !rows.Next() {
210210
rows.Close()
211211
b.Fatalf("crashed")
@@ -232,9 +232,9 @@ func BenchmarkInterpolation(b *testing.B) {
232232
maxAllowedPacket: maxPacketSize,
233233
maxWriteSize: maxPacketSize - 1,
234234
}
235-
235+
236236
buf := newBuffer(nil)
237-
mc.reader = newSimpleReader(&buf)
237+
mc.reader = &buf
238238

239239
args := []driver.Value{
240240
int64(42424242),

buffer.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const defaultBufSize = 4096
2121
// In other words, we can't write and read simultaneously on the same connection.
2222
// The buffer is similar to bufio.Reader / Writer but zero-copy-ish
2323
// Also highly optimized for this particular use case.
24-
type buffer struct { //PROBLEM: figure this all out better
24+
type buffer struct {
2525
buf []byte
2626
nc net.Conn
2727
idx int
@@ -49,7 +49,7 @@ func (b *buffer) fill(need int) error {
4949
// grow buffer if necessary
5050
// TODO: let the buffer shrink again at some point
5151
// Maybe keep the org buf slice and swap back?
52-
if need > len(b.buf) { //look up what len and cap mean again!
52+
if need > len(b.buf) {
5353
// Round up to the next multiple of the default size
5454
newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
5555
copy(newBuf, b.buf)
@@ -92,10 +92,6 @@ func (b *buffer) fill(need int) error {
9292
// returns next N bytes from buffer.
9393
// The returned slice is only guaranteed to be valid until the next read
9494
func (b *buffer) readNext(need int) ([]byte, error) {
95-
if need == -1 {
96-
return b.takeCompleteBuffer()
97-
}
98-
9995
if b.length < need {
10096
// refill
10197
if err := b.fill(need); err != nil {
@@ -113,8 +109,12 @@ func (b *buffer) readNext(need int) ([]byte, error) {
113109
// If possible, a slice from the existing buffer is returned.
114110
// Otherwise a bigger buffer is made.
115111
// Only one buffer (total) can be used at a time.
116-
func (b *buffer) takeBuffer(length int) []byte {
117-
if b.length > 0 { //assume its empty
112+
func (b *buffer) reuseBuffer(length int) []byte {
113+
if length == -1 {
114+
return b.takeCompleteBuffer()
115+
}
116+
117+
if b.length > 0 {
118118
return nil
119119
}
120120

@@ -130,18 +130,6 @@ func (b *buffer) takeBuffer(length int) []byte {
130130
return make([]byte, length)
131131
}
132132

133-
/*
134-
// shortcut which can be used if the requested buffer is guaranteed to be
135-
// smaller than defaultBufSize
136-
// Only one buffer (total) can be used at a time.
137-
func (b *buffer) takeSmallBuffer(length int) []byte {
138-
if b.length == 0 { //assume its empty
139-
return b.buf[:length]
140-
}
141-
return nil
142-
}
143-
*/
144-
145133
// takeCompleteBuffer returns the complete existing buffer.
146134
// This can be used if the necessary buffer size is unknown.
147135
// Only one buffer (total) can be used at a time.

compress.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,26 @@ const (
1111
)
1212

1313
type compressedReader struct {
14-
buf *buffer //packetReader
14+
buf packetReader
1515
bytesBuf []byte
1616
mc *mysqlConn
1717
zr io.ReadCloser
1818
}
1919

20-
21-
type simpleReader struct {
22-
buf *buffer //packetReader
23-
}
24-
2520
type compressedWriter struct {
2621
connWriter io.Writer
2722
mc *mysqlConn
2823
zw *zlib.Writer
2924
}
3025

31-
func newCompressedReader(buf *buffer, mc *mysqlConn) *compressedReader {
26+
func newCompressedReader(buf packetReader, mc *mysqlConn) *compressedReader {
3227
return &compressedReader{
3328
buf: buf,
3429
bytesBuf: make([]byte, 0),
3530
mc: mc,
3631
}
3732
}
3833

39-
func newSimpleReader(buf *buffer) *simpleReader {
40-
return &simpleReader{
41-
buf: buf,
42-
}
43-
}
44-
4534
func newCompressedWriter(connWriter io.Writer, mc *mysqlConn) *compressedWriter {
4635
return &compressedWriter{
4736
connWriter: connWriter,
@@ -63,8 +52,8 @@ func (cr *compressedReader) readNext(need int) ([]byte, error) {
6352
return data, nil
6453
}
6554

66-
func (sr *simpleReader) readNext(need int) ([]byte, error) {
67-
return sr.buf.readNext(need)
55+
func (cr *compressedReader) reuseBuffer(length int) []byte {
56+
return cr.buf.reuseBuffer(length)
6857
}
6958

7059
func (cr *compressedReader) uncompressPacket() error {

compress_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func (mb *mockBuf) readNext(need int) ([]byte, error) {
3939
return data, nil
4040
}
4141

42+
func (mb *mockBuf) reuseBuffer(length int) []byte {
43+
return make([]byte, length) //just give them a new buffer
44+
}
45+
4246
// compressHelper compresses uncompressedPacket and checks state variables
4347
func compressHelper(t *testing.T, mc *mysqlConn, uncompressedPacket []byte) []byte {
4448
// get status variables

connection.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,9 @@ type mysqlConn struct {
5454

5555
type packetReader interface {
5656
readNext(need int) ([]byte, error)
57+
reuseBuffer(length int) []byte
5758
}
5859

59-
/*
60-
type packetReadCloser interface{
61-
Read(n int) ([]byte, error)
62-
Close() error // PROBLEM: is there a way to do this?
63-
}
64-
65-
type packetWriteCloser interface{
66-
Write([]byte) (int, error)
67-
Close() error
68-
}
69-
*/
70-
7160
// Handles parameters set in DSN after the connection is established
7261
func (mc *mysqlConn) handleParams() (err error) {
7362
for param, val := range mc.cfg.Params {
@@ -208,10 +197,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
208197
return "", driver.ErrSkip
209198
}
210199

211-
//https://stackoverflow.com/questions/29684609/how-to-check-if-an-object-has-a-particular-method
212-
213-
//reader has &buf which is a packetreader but also always a buffer
214-
buf, _ := mc.reader.readNext(-1) //PROBLEM uncompressed so this works, what if compressed
200+
buf := mc.reader.reuseBuffer(-1)
215201

216202
if buf == nil {
217203
// can not take the buffer. Something must be wrong with the connection

connection_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestInterpolateParams(t *testing.T) {
2121
},
2222
}
2323
buf := newBuffer(nil)
24-
mc.reader = newSimpleReader(&buf)
24+
mc.reader = &buf
2525

2626
q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42), "gopher"})
2727
if err != nil {
@@ -42,7 +42,7 @@ func TestInterpolateParamsTooManyPlaceholders(t *testing.T) {
4242
},
4343
}
4444
buf := newBuffer(nil)
45-
mc.reader = newSimpleReader(&buf)
45+
mc.reader = &buf
4646

4747
q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42)})
4848
if err != driver.ErrSkip {
@@ -61,7 +61,7 @@ func TestInterpolateParamsPlaceholderInString(t *testing.T) {
6161
}
6262

6363
buf := newBuffer(nil)
64-
mc.reader = newSimpleReader(&buf)
64+
mc.reader = &buf
6565

6666
q, err := mc.interpolateParams("SELECT 'abc?xyz',?", []driver.Value{int64(42)})
6767
// When InterpolateParams support string literal, this should return `"SELECT 'abc?xyz', 42`

driver.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
9898
mc.writeTimeout = mc.cfg.WriteTimeout
9999

100100
// packet reader and writer in handshake are never compressed
101-
mc.reader = newSimpleReader(&buf)
101+
mc.reader = &buf
102102
mc.writer = mc.netConn
103103

104-
105104
// Reading Handshake Initialization Packet
106105
cipher, err := mc.readInitPacket()
107106
if err != nil {

packets.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
2828
var prevData []byte
2929
for {
3030
// read packet header
31-
data, err := mc.reader.readNext(4)
31+
data, err := mc.reader.readNext(4)
3232
if err != nil {
3333
if cerr := mc.canceled.Value(); cerr != nil {
3434
return nil, cerr
@@ -64,7 +64,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
6464
}
6565

6666
// read packet body [pktLen bytes]
67-
data, err = mc.reader.readNext(pktLen)
67+
data, err = mc.reader.readNext(pktLen)
6868
if err != nil {
6969
if cerr := mc.canceled.Value(); cerr != nil {
7070
return nil, cerr
@@ -283,8 +283,8 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
283283
}
284284

285285
// Calculate packet length and get buffer with that size
286-
data, _ := mc.reader.readNext(pktLen + 4)
287-
286+
data := mc.reader.reuseBuffer(pktLen + 4)
287+
288288
if data == nil {
289289
// can not take the buffer. Something must be wrong with the connection
290290
errLog.Print(ErrBusyBuffer)
@@ -329,10 +329,9 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
329329
mc.netConn = tlsConn
330330
nc := tlsConn
331331

332-
// make newBuffer with tls conn, clean slate bc handshake
333332
newBuf := newBuffer(nc)
334-
mc.reader = newSimpleReader(&newBuf)
335-
333+
mc.reader = &newBuf
334+
336335
mc.writer = mc.netConn
337336
}
338337

@@ -378,7 +377,7 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
378377

379378
// Calculate the packet length and add a tailing 0
380379
pktLen := len(scrambleBuff) + 1
381-
data, _ := mc.reader.readNext(4 + pktLen)
380+
data := mc.reader.reuseBuffer(4 + pktLen)
382381

383382
if data == nil {
384383
// can not take the buffer. Something must be wrong with the connection
@@ -398,8 +397,8 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
398397
func (mc *mysqlConn) writeClearAuthPacket() error {
399398
// Calculate the packet length and add a tailing 0
400399
pktLen := len(mc.cfg.Passwd) + 1
401-
data, _ := mc.reader.readNext(4 + pktLen)
402-
400+
data := mc.reader.reuseBuffer(4 + pktLen)
401+
403402
if data == nil {
404403
// can not take the buffer. Something must be wrong with the connection
405404
errLog.Print(ErrBusyBuffer)
@@ -422,8 +421,8 @@ func (mc *mysqlConn) writeNativeAuthPacket(cipher []byte) error {
422421

423422
// Calculate the packet length and add a tailing 0
424423
pktLen := len(scrambleBuff)
425-
data, _ := mc.reader.readNext(4 + pktLen)
426-
424+
data := mc.reader.reuseBuffer(4 + pktLen)
425+
427426
if data == nil {
428427
// can not take the buffer. Something must be wrong with the connection
429428
errLog.Print(ErrBusyBuffer)
@@ -445,8 +444,8 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
445444
mc.sequence = 0
446445
mc.compressionSequence = 0
447446

448-
data, _ := mc.reader.readNext(4+1)
449-
447+
data := mc.reader.reuseBuffer(4 + 1)
448+
450449
if data == nil {
451450
// can not take the buffer. Something must be wrong with the connection
452451
errLog.Print(ErrBusyBuffer)
@@ -466,8 +465,8 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
466465
mc.compressionSequence = 0
467466

468467
pktLen := 1 + len(arg)
469-
data, _ := mc.reader.readNext(pktLen + 4)
470-
468+
data := mc.reader.reuseBuffer(pktLen + 4)
469+
471470
if data == nil {
472471
// can not take the buffer. Something must be wrong with the connection
473472
errLog.Print(ErrBusyBuffer)
@@ -489,8 +488,8 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
489488
mc.sequence = 0
490489
mc.compressionSequence = 0
491490

492-
data, _ := mc.reader.readNext(4 + 1 + 4)
493-
491+
data := mc.reader.reuseBuffer(4 + 1 + 4)
492+
494493
if data == nil {
495494
// can not take the buffer. Something must be wrong with the connection
496495
errLog.Print(ErrBusyBuffer)
@@ -957,9 +956,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
957956
var data []byte
958957

959958
if len(args) == 0 {
960-
data, _ = mc.reader.readNext(minPktLen)
959+
data = mc.reader.reuseBuffer(minPktLen)
960+
961961
} else {
962-
data, _ = mc.reader.readNext(-1) //how does this work out with compressed?
962+
data = mc.reader.reuseBuffer(-1)
963963
}
964964
if data == nil {
965965
// can not take the buffer. Something must be wrong with the connection
@@ -1138,8 +1138,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
11381138
// In that case we must build the data packet with the new values buffer
11391139
if valuesCap != cap(paramValues) {
11401140
data = append(data[:pos], paramValues...)
1141-
readerBuffer := mc.reader.getBuffer() //PROBLEM: what the fuck is going on here??
1142-
readerBuffer.buf = data
1141+
1142+
bufBuf := mc.reader.reuseBuffer(-1)
1143+
bufBuf = data
1144+
fmt.Println(bufBuf) //dont know how to make it compile w/o some op here on bufBuf
11431145
}
11441146

11451147
pos += len(paramValues)

0 commit comments

Comments
 (0)