Skip to content

Commit 6629b93

Browse files
committed
introduce mysqlConn.data
1 parent 9d248ae commit 6629b93

File tree

3 files changed

+12
-30
lines changed

3 files changed

+12
-30
lines changed

auth.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,12 @@ func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
361361
pubKey := mc.cfg.pubKey
362362
if pubKey == nil {
363363
// request public key from server
364-
data, err := mc.buf.takeSmallBuffer(4 + 1)
365-
if err != nil {
366-
return err
367-
}
368-
data[4] = cachingSha2PasswordRequestPublicKey
369-
err = mc.writePacket(data)
370-
if err != nil {
364+
if err := mc.writeCommandPacket(cachingSha2PasswordRequestPublicKey); err != nil {
371365
return err
372366
}
373367

374-
if data, err = mc.readPacket(); err != nil {
368+
data, err := mc.readPacket()
369+
if err != nil {
375370
return err
376371
}
377372

connection.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type mysqlConn struct {
5454
canceled atomicError // set non-nil if conn is canceled
5555
closed atomicBool // set when conn is closed, before closech is closed
5656

57+
data [16]byte // buffer for small reads and writes
5758
readRes chan readResult // channel for read result
5859
writeReq chan []byte // buffered channel for write packets
5960
writeRes chan writeResult // channel for write result

packets.go

+8-22
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,11 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
417417
// Reset Packet Sequence
418418
mc.sequence = 0
419419

420-
data, err := mc.buf.takeSmallBuffer(4 + 1)
421-
if err != nil {
422-
// cannot take the buffer. Something must be wrong with the connection
423-
mc.cfg.Logger.Print(err)
424-
return errBadConnNoWrite
425-
}
426-
427420
// Add command byte
428-
data[4] = command
421+
mc.data[4] = command
429422

430423
// Send CMD packet
431-
return mc.writePacket(data)
424+
return mc.writePacket(mc.data[:5])
432425
}
433426

434427
func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
@@ -457,24 +450,17 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
457450
// Reset Packet Sequence
458451
mc.sequence = 0
459452

460-
data, err := mc.buf.takeSmallBuffer(4 + 1 + 4)
461-
if err != nil {
462-
// cannot take the buffer. Something must be wrong with the connection
463-
mc.cfg.Logger.Print(err)
464-
return errBadConnNoWrite
465-
}
466-
467453
// Add command byte
468-
data[4] = command
454+
mc.data[4] = command
469455

470456
// Add arg [32 bit]
471-
data[5] = byte(arg)
472-
data[6] = byte(arg >> 8)
473-
data[7] = byte(arg >> 16)
474-
data[8] = byte(arg >> 24)
457+
mc.data[5] = byte(arg)
458+
mc.data[6] = byte(arg >> 8)
459+
mc.data[7] = byte(arg >> 16)
460+
mc.data[8] = byte(arg >> 24)
475461

476462
// Send CMD packet
477-
return mc.writePacket(data)
463+
return mc.writePacket(mc.data[:4+1+4])
478464
}
479465

480466
/******************************************************************************

0 commit comments

Comments
 (0)