Skip to content

Commit 309703a

Browse files
committed
fix(timeout): execute add timeout option
1 parent 6bc6d86 commit 309703a

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/commands/execute.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Execute extends Command {
1515
this.onResult = callback;
1616
this.parameters = options.values;
1717
this.insertId = 0;
18+
this.timeout = options.timeout;
19+
this.queryTimeout = null;
1820
this._rows = [];
1921
this._fields = [];
2022
this._result = [];
@@ -35,6 +37,7 @@ class Execute extends Command {
3537
start(packet, connection) {
3638
this._connection = connection;
3739
this.options = Object.assign({}, connection.config, this._executeOptions);
40+
this._setTimeout();
3841
const executePacket = new Packets.Execute(
3942
this.statement.id,
4043
this.parameters,
@@ -96,6 +99,8 @@ Execute.prototype.resultsetHeader = Query.prototype.resultsetHeader;
9699
Execute.prototype._findOrCreateReadStream =
97100
Query.prototype._findOrCreateReadStream;
98101
Execute.prototype._streamLocalInfile = Query.prototype._streamLocalInfile;
102+
Execute.prototype._setTimeout = Query.prototype._setTimeout;
103+
Execute.prototype._handleTimeoutError = Query.prototype._handleTimeoutError;
99104
Execute.prototype.row = Query.prototype.row;
100105
Execute.prototype.stream = Query.prototype.stream;
101106

lib/commands/query.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ class Query extends Command {
5151
}
5252
this._connection = connection;
5353
this.options = Object.assign({}, connection.config, this._queryOptions);
54-
55-
if (this.timeout) {
56-
const timeoutHandler = this._handleTimeoutError.bind(this);
57-
this.queryTimeout = Timers.setTimeout(
58-
timeoutHandler,
59-
this.timeout
60-
);
61-
}
54+
this._setTimeout();
6255

6356
const cmdPacket = new Packets.Query(
6457
this.sql,
@@ -294,6 +287,16 @@ class Query extends Command {
294287
return stream;
295288
}
296289

290+
_setTimeout() {
291+
if (this.timeout) {
292+
const timeoutHandler = this._handleTimeoutError.bind(this);
293+
this.queryTimeout = Timers.setTimeout(
294+
timeoutHandler,
295+
this.timeout
296+
);
297+
}
298+
}
299+
297300
_handleTimeoutError() {
298301
if (this.queryTimeout) {
299302
Timers.clearTimeout(this.queryTimeout);

test/integration/connection/test-query-timeout.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,20 @@ connection.query({ sql: 'SELECT sleep(1) as a', timeout: 5000 }, (err, res) => {
1717

1818
connection.query('SELECT sleep(1) as a', (err, res) => {
1919
assert.deepEqual(res, [{ a: 0 }]);
20+
});
21+
22+
connection.execute({ sql: 'SELECT sleep(3) as a', timeout: 500 }, (err, res) => {
23+
assert.equal(res, null);
24+
assert.ok(err);
25+
assert.equal(err.code, 'PROTOCOL_SEQUENCE_TIMEOUT');
26+
assert.equal(err.message, 'Query inactivity timeout');
27+
});
28+
29+
connection.execute({ sql: 'SELECT sleep(1) as a', timeout: 5000 }, (err, res) => {
30+
assert.deepEqual(res, [{ a: 0 }]);
31+
});
32+
33+
connection.execute('SELECT sleep(1) as a', (err, res) => {
34+
assert.deepEqual(res, [{ a: 0 }]);
2035
connection.end();
2136
});

0 commit comments

Comments
 (0)