Skip to content

Commit

Permalink
Implement CMD_RESET_CONNECTION
Browse files Browse the repository at this point in the history
  • Loading branch information
testn committed Oct 28, 2021
1 parent 3c300a8 commit 299ed3d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const RegisterSlave = require('./register_slave.js');
const BinlogDump = require('./binlog_dump.js');
const ChangeUser = require('./change_user.js');
const Quit = require('./quit.js');
const ResetConnection = require('./reset_connection.js');

module.exports = {
ClientHandshake,
Expand All @@ -23,5 +24,6 @@ module.exports = {
RegisterSlave,
BinlogDump,
ChangeUser,
Quit
Quit,
ResetConnection
};
26 changes: 26 additions & 0 deletions lib/commands/reset_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const Command = require('./command');
const Packets = require('../packets/index.js');

class ResetConnection extends Command {
constructor(callback) {
super();
this.onResult = callback;
}

start(packet, connection) {
const req = new Packets.ResetConnection();
connection.writePacket(req.toPacket());
return ResetConnection.prototype.resetConnectionResponse;
}

resetConnectionResponse() {
if (this.onResult) {
process.nextTick(this.onResult.bind(this));
}
return null;
}
}

module.exports = ResetConnection;
4 changes: 4 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,10 @@ class Connection extends EventEmitter {
return this.addCommand(new Commands.Ping(cb));
}

reset(cb) {
return this.addCommand(new Commands.ResetConnection(cb));
}

_registerSlave(opts, cb) {
return this.addCommand(new Commands.RegisterSlave(opts, cb));
}
Expand Down
1 change: 1 addition & 0 deletions lib/constants/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ module.exports = {
STMT_FETCH: 0x1c,
DAEMON: 0x1d, // deprecated
BINLOG_DUMP_GTID: 0x1e,
RESET_CONNECTION: 0x1f, // introduced in 5.7.3
UNKNOWN: 0xff // bad!
};
4 changes: 3 additions & 1 deletion lib/packets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const RegisterSlave = require('./register_slave');
const ResultSetHeader = require('./resultset_header');
const SSLRequest = require('./ssl_request');
const TextRow = require('./text_row');
const ResetConnection = require('./reset_connection');

const ctorMap = {
AuthSwitchRequest,
Expand All @@ -44,7 +45,8 @@ const ctorMap = {
RegisterSlave,
ResultSetHeader,
SSLRequest,
TextRow
TextRow,
ResetConnection
};
Object.entries(ctorMap).forEach(([name, ctor]) => {
module.exports[name] = ctor;
Expand Down
18 changes: 18 additions & 0 deletions lib/packets/reset_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const Packet = require('../packets/packet');
const CommandCodes = require('../constants/commands');

class ResetConnection {
constructor() {
}

toPacket() {
const packet = new Packet(0, Buffer.allocUnsafe(5), 0, 5);
packet.offset = 4;
packet.writeInt8(CommandCodes.RESET_CONNECTION);
return packet;
}
}

module.exports = ResetConnection;
9 changes: 9 additions & 0 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ class PromiseConnection extends EventEmitter {
});
}

reset() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
c.reset(done);
});
}

connect() {
const c = this.connection;
const localErr = new Error();
Expand Down

0 comments on commit 299ed3d

Please sign in to comment.