diff --git a/lib/connection.js b/lib/connection.js index e2720be078..9f87c3a9e0 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -20,7 +20,7 @@ const Tls = require('tls'); const Timers = require('timers'); const EventEmitter = require('events').EventEmitter; const Readable = require('stream').Readable; -const Queue = require('denque'); +const Queue = require('js-sdsl').Deque; const SqlString = require('sqlstring'); const LRU = require('lru-cache'); @@ -223,12 +223,14 @@ class Connection extends EventEmitter { !( this._command && this._command.constructor === Commands.ClientHandshake && - this._commands.length > 0 + !this._commands.empty() ) ) { bubbleErrorToConnection = true; } - while ((command = this._commands.shift())) { + while (!this._commands.empty()) { + command = this._commands.front(); + this._commands.popFront(); if (command.onResult) { command.onResult(err); } else { @@ -404,7 +406,7 @@ class Connection extends EventEmitter { handlePacket(packet) { if (this._paused) { - this._paused_packets.push(packet); + this._paused_packets.pushBack(packet); return; } if (packet) { @@ -460,7 +462,8 @@ class Connection extends EventEmitter { } const done = this._command.execute(packet, this); if (done) { - this._command = this._commands.shift(); + this._command = this._commands.front(); + this._commands.popFront(); if (this._command) { this.sequenceId = 0; this.compressedSequenceId = 0; @@ -482,7 +485,7 @@ class Connection extends EventEmitter { this._command = cmd; this.handlePacket(); } else { - this._commands.push(cmd); + this._commands.pushBack(cmd); } return cmd; } @@ -559,7 +562,9 @@ class Connection extends EventEmitter { resume() { let packet; this._paused = false; - while ((packet = this._paused_packets.shift())) { + while (!this._paused_packets.empty()) { + packet = this._paused_packets.front(); + this._paused_packets.popFront(); this.handlePacket(packet); // don't resume if packet handler paused connection if (this._paused) { diff --git a/lib/pool.js b/lib/pool.js index 6a7ab481a2..58e4e15bc8 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -5,14 +5,14 @@ const mysql = require('../index.js'); const EventEmitter = require('events').EventEmitter; const PoolConnection = require('./pool_connection.js'); -const Queue = require('denque'); +const Queue = require('js-sdsl').Deque; const Connection = require('./connection.js'); function spliceConnection(queue, connection) { - const len = queue.length; + const len = queue.size(); for (let i = 0; i < len; i++) { - if (queue.get(i) === connection) { - queue.removeOne(i); + if (queue.getElementByPos(i) === connection) { + queue.eraseElementByPos(i); break; } } @@ -39,19 +39,20 @@ class Pool extends EventEmitter { return process.nextTick(() => cb(new Error('Pool is closed.'))); } let connection; - if (this._freeConnections.length > 0) { - connection = this._freeConnections.shift(); + if (!this._freeConnections.empty()) { + const connection = this._freeConnections.front(); + this._freeConnections.popFront(); this.emit('acquire', connection); return process.nextTick(() => cb(null, connection)); } if ( this.config.connectionLimit === 0 || - this._allConnections.length < this.config.connectionLimit + this._allConnections.size() < this.config.connectionLimit ) { connection = new PoolConnection(this, { config: this.config.connectionConfig }); - this._allConnections.push(connection); + this._allConnections.pushBack(connection); return connection.connect(err => { if (this._closed) { return cb(new Error('Pool is closed.')); @@ -69,27 +70,29 @@ class Pool extends EventEmitter { } if ( this.config.queueLimit && - this._connectionQueue.length >= this.config.queueLimit + this._connectionQueue.size() >= this.config.queueLimit ) { return cb(new Error('Queue limit reached.')); } this.emit('enqueue'); - return this._connectionQueue.push(cb); + return this._connectionQueue.pushBack(cb); } releaseConnection(connection) { let cb; if (!connection._pool) { // The connection has been removed from the pool and is no longer good. - if (this._connectionQueue.length) { - cb = this._connectionQueue.shift(); + if (!this._connectionQueue.empty()) { + cb = this._connectionQueue.front(); + this._connectionQueue.popFront(); process.nextTick(this.getConnection.bind(this, cb)); } - } else if (this._connectionQueue.length) { - cb = this._connectionQueue.shift(); + } else if (!this._connectionQueue.empty()) { + cb = this._connectionQueue.front(); + this._connectionQueue.popFront(); process.nextTick(cb.bind(null, null, connection)); } else { - this._freeConnections.push(connection); + this._freeConnections.pushBack(connection); this.emit('release', connection); } } @@ -110,18 +113,19 @@ class Pool extends EventEmitter { if (calledBack) { return; } - if (err || ++closedConnections >= this._allConnections.length) { + if (err || ++closedConnections >= this._allConnections.size()) { calledBack = true; cb(err); return; } }.bind(this); - if (this._allConnections.length === 0) { + if (this._allConnections.empty()) { endCB(); return; } - for (let i = 0; i < this._allConnections.length; i++) { - connection = this._allConnections.get(i); + const length = this._allConnections.size(); + for (let i = 0; i < length; i++) { + connection = this._allConnections.getElementByPos(i); connection._realEnd(endCB); } } diff --git a/package-lock.json b/package-lock.json index 9fecec0b42..dabebaeccd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,9 +11,9 @@ "dependencies": { "@types/chai": "^4.3.3", "chai": "^4.3.6", - "denque": "^2.0.1", "generate-function": "^2.3.1", "iconv-lite": "^0.6.3", + "js-sdsl": "^4.1.4", "long": "^4.0.0", "lru-cache": "^6.0.0", "mocha": "^10.0.0", @@ -968,14 +968,6 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, - "node_modules/denque": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/denque/-/denque-2.0.1.tgz", - "integrity": "sha512-tfiWc6BQLXNLpNiR5iGd0Ocu3P3VpxfzFiqubLgMfhfOw9WyvgJBd46CClNn9k3qfbjvT//0cf7AlYRX/OslMQ==", - "engines": { - "node": ">=0.10" - } - }, "node_modules/diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -1981,6 +1973,11 @@ "node": ">=8" } }, + "node_modules/js-sdsl": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.1.4.tgz", + "integrity": "sha512-Y2/yD55y5jteOAmY50JbUZYwk3CP3wnLPEZnlR1w9oKhITrBEtAxwuWKebFf8hMrPMgbYwFoWK/lH2sBkErELw==" + }, "node_modules/js-tokens": { "version": "4.0.0", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", @@ -4248,11 +4245,6 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, - "denque": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/denque/-/denque-2.0.1.tgz", - "integrity": "sha512-tfiWc6BQLXNLpNiR5iGd0Ocu3P3VpxfzFiqubLgMfhfOw9WyvgJBd46CClNn9k3qfbjvT//0cf7AlYRX/OslMQ==" - }, "diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -4970,6 +4962,11 @@ "istanbul-lib-report": "^3.0.0" } }, + "js-sdsl": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.1.4.tgz", + "integrity": "sha512-Y2/yD55y5jteOAmY50JbUZYwk3CP3wnLPEZnlR1w9oKhITrBEtAxwuWKebFf8hMrPMgbYwFoWK/lH2sBkErELw==" + }, "js-tokens": { "version": "4.0.0", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", diff --git a/package.json b/package.json index 4bef6ee209..643b564c7a 100644 --- a/package.json +++ b/package.json @@ -57,9 +57,9 @@ "dependencies": { "@types/chai": "^4.3.3", "chai": "^4.3.6", - "denque": "^2.0.1", "generate-function": "^2.3.1", "iconv-lite": "^0.6.3", + "js-sdsl": "^4.1.4", "long": "^4.0.0", "lru-cache": "^6.0.0", "mocha": "^10.0.0",