sidorares / sidorares/node-mysql2
Warning: got packets out of order
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 4.4k
- Forks
- 680
- Avg merge
- 9h 7m
- Merged PRs (30d)
- 59
Description
Hi,
I'm trying to combine the node-mysql2 and ssh2 modules so that I can connect to the database via ssh. Something similar to this:
https://stackoverflow.com/questions/30658906/trouble-connecting-to-mysql-via-ssh/30669454#30669454
Everything is working as expected. However, if I try to run several queries concurrently, then I get a got packets out of order warning and eventually an error.
Here is some code that will reproduce the issue:
db.js
'use strict';
const Bluebird = require('bluebird');
const Mysql = require('mysql2/promise');
module.exports = function (stream) {
const connectionOptions = {
host: 'localhost',
user: 'root',
password: 'YOUR_PASSWORD',
database: 'YOUR_DB',
connectionLimit: 10,
dateStrings: true,
decimalNumbers: true,
timezone: 'Z',
supportBigNumbers: true,
stream: stream,
Promise: Bluebird
};
const pool = Mysql.createPool(connectionOptions);
function getSqlConnection() {
return pool.getConnection().disposer(function (connection) {
return connection.release();
});
}
return {
query(sqlString, values) {
return Bluebird.using(getSqlConnection(), function (connection) {
return connection.query(sqlString, values);
});
},
closeAllConections() {
return pool.end(function (err) {
if (err) {
console.log(err);
}
});
}
};
};
index.js
'use strict';
const _ = require('lodash');
const Bluebird = require('bluebird');
const SshClient = require('ssh2').Client;
const ssh = new SshClient();
function runDbQueries() {
return new Bluebird(function (resolve, reject) {
ssh.on('ready', function () {
ssh.forwardOut(
'127.0.0.1',
12345,
'127.0.0.1',
3306,
function (err, stream) {
if (err) {
return reject(err);
}
const Db = require('./db.js')(stream);
const promises = _.map(_.range(5), function (n) {
return Db.query('Select * from `users`', []);
});
return resolve(Bluebird.all(promises));
}
);
});
ssh.connect({
host: '127.0.0.1',
username: 'vagrant',
agent: process.env.SSH_AUTH_SOCK
});
});
}
runDbQueries()
.then(function (results) {
console.log(results);
})
.catch(function (error) {
console.log(error);
});
If I run the queries in series, then the code works just fine. Also, I have similar code running without the ssh2 requirement, and the concurrent queries run without problems there.
Any help would be appreciated. Thanks.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the connection and pool setup in db.js, then trace how the single SSH stream from index.js is used while the five queries run concurrently. Reproduce the warning with the supplied example and compare it with serial execution; done means concurrent queries through the SSH connection no longer produce packet-order errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mysql, node.js
- Domain
- databases, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100