Skip to content

Commit

Permalink
[misc] small performance improvement to avoid testing debug each packet
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 5, 2019
1 parent 74ceab5 commit 0f542cc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ function Connection(options) {
*/
this.debug = val => {
opts.debug = val;
opts.emit('debug', opts.logPackets, opts.debug);
};

this.debugCompress = val => {
Expand Down
24 changes: 22 additions & 2 deletions lib/io/packet-input-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class PacketInputStream {
this.parts = null;
this.partsTotalLen = 0;
this.changeEncoding(this.opts.collation);
this.changeDebug(this.opts.logPackets, this.opts.debug);
this.opts.on('collation', this.changeEncoding.bind(this));
this.opts.on('debug', this.changeDebug.bind(this));
}

changeEncoding(collation) {
Expand All @@ -36,10 +38,17 @@ class PacketInputStream {
: PacketIconvEncoded;
}

receivePacket(packet) {
changeDebug(logPackets, debug) {
this.logPackets = logPackets;
this.debug = debug;
this.receivePacket =
this.logPackets || this.debug ? this.receivePacketDebug : this.receivePacketBasic;
}

receivePacketDebug(packet) {
let cmd = this.currentCmd();

if (packet && (this.opts.logPackets || this.opts.debug)) {
if (packet) {
const packetStr = Utils.log(this.opts, packet.buf, packet.pos, packet.end, this.header);
if (this.opts.logPackets) {
this.info.addPacket(
Expand Down Expand Up @@ -85,6 +94,17 @@ class PacketInputStream {
if (!cmd.onPacketReceive) this.receiveQueue.shift();
}

receivePacketBasic(packet) {
let cmd = this.currentCmd();
if (!cmd) {
this.unexpectedPacket(packet);
return;
}
cmd.sequenceNo = this.header[3];
cmd.onPacketReceive(packet, this.out, this.opts, this.info);
if (!cmd.onPacketReceive) this.receiveQueue.shift();
}

resetHeader() {
this.remainingLen = null;
this.headerLen = 0;
Expand Down
72 changes: 50 additions & 22 deletions lib/io/packet-output-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class PacketOutputStream {
this.pos = 4;
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
this.changeEncoding(this.opts.collation);
this.changeDebug(this.opts.logPackets, this.opts.debug);

this.opts.on('collation', this.changeEncoding.bind(this));
this.opts.on('debug', this.changeDebug.bind(this));
}

changeEncoding(collation) {
Expand All @@ -47,6 +50,13 @@ class PacketOutputStream {
}
}

changeDebug(logPackets, debug) {
this.logPackets = logPackets;
this.debug = debug;
this.flushBuffer =
this.logPackets || this.debug ? this.flushBufferDebug : this.flushBufferBasic;
}

setStream(stream) {
this.stream = stream;
}
Expand Down Expand Up @@ -381,39 +391,57 @@ class PacketOutputStream {
/**
* Flush the internal buffer.
*/
flushBuffer(commandEnd, remainingLen) {
flushBufferDebug(commandEnd, remainingLen) {
this.buf[0] = this.pos - 4;
this.buf[1] = (this.pos - 4) >>> 8;
this.buf[2] = (this.pos - 4) >>> 16;
this.buf[3] = ++this.cmd.sequenceNo;

this.stream.writeBuf(this.buf.slice(0, this.pos), this.cmd);

if (this.opts.logPackets || this.opts.debug) {
const packet = Utils.log(this.opts, this.buf, 0, this.pos);
if (this.opts.logPackets) {
this.info.addPacket(
'==> conn:' +
(this.info.threadId ? this.info.threadId : -1) +
' ' +
this.cmd.constructor.name +
'(0,' +
this.pos +
')\n' +
packet
);
}
const packet = Utils.log(this.opts, this.buf, 0, this.pos);
if (this.opts.logPackets) {
this.info.addPacket(
'==> conn:' +
(this.info.threadId ? this.info.threadId : -1) +
' ' +
this.cmd.constructor.name +
'(0,' +
this.pos +
')\n' +
packet
);
}

if (this.opts.debug) {
console.log(
'==> conn:%d %s\n%s',
this.info.threadId ? this.info.threadId : -1,
this.cmd.constructor.name + '(0,' + this.pos + ')',
Utils.log(this.opts, this.buf, 0, this.pos)
);
if (this.opts.debug) {
console.log(
'==> conn:%d %s\n%s',
this.info.threadId ? this.info.threadId : -1,
this.cmd.constructor.name + '(0,' + this.pos + ')',
Utils.log(this.opts, this.buf, 0, this.pos)
);
}

if (commandEnd) {
//if last packet fill the max size, must send an empty com to indicate that command end.
if (this.pos === MAX_BUFFER_SIZE) {
this.writeEmptyPacket();
} else {
this.stream.flush(true, this.cmd);
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
}
} else {
this.buf = allocateBuffer(remainingLen + 4);
this.pos = 4;
}
}

flushBufferBasic(commandEnd, remainingLen) {
this.buf[0] = this.pos - 4;
this.buf[1] = (this.pos - 4) >>> 8;
this.buf[2] = (this.pos - 4) >>> 16;
this.buf[3] = ++this.cmd.sequenceNo;
this.stream.writeBuf(this.buf.slice(0, this.pos), this.cmd);
if (commandEnd) {
//if last packet fill the max size, must send an empty com to indicate that command end.
if (this.pos === MAX_BUFFER_SIZE) {
Expand Down
4 changes: 3 additions & 1 deletion test/integration/test-ssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('ssl', function() {
if (process.env.MAXSCALE_VERSION) this.skip();
if (
tls.DEFAULT_MIN_VERSION === 'TLSv1.2' &&
((process.platform === 'win32' && shareConn.info.isMariaDB() && !shareConn.info.hasMinVersion(10, 4, 0)) ||
((process.platform === 'win32' &&
shareConn.info.isMariaDB() &&
!shareConn.info.hasMinVersion(10, 4, 0)) ||
(!shareConn.info.isMariaDB() && !shareConn.info.hasMinVersion(8, 0, 0)))
) {
//TLSv1.2 is supported on windows only since MariaDB 10.4
Expand Down

0 comments on commit 0f542cc

Please sign in to comment.