Skip to content

Commit c76b381

Browse files
committed
Lints Pool.js
1 parent 43a570c commit c76b381

File tree

1 file changed

+65
-47
lines changed

1 file changed

+65
-47
lines changed

lib/Pool.js

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
2-
var Mysql = require('../');
1+
var mysql = require('../');
32
var Connection = require('./Connection');
43

54
module.exports = Pool;
5+
66
function Pool(options) {
77
this.config = options.config;
88
this.config.connectionConfig.pool = this;
@@ -13,81 +13,94 @@ function Pool(options) {
1313
this._closed = false;
1414
}
1515

16-
Pool.prototype.getConnection = function(cb) {
16+
Pool.prototype.getConnection = function (cb) {
1717
if (this._closed) {
18-
cb(new Error('Pool is closed.'));
19-
return;
18+
return cb(new Error('Pool is closed.'));
2019
}
2120

21+
var connection;
22+
2223
if (this._freeConnections.length > 0) {
23-
var connection = this._freeConnections[0];
24-
this._freeConnections.shift();
25-
cb(null, connection);
26-
} else if (this.config.connectionLimit == 0 || this._allConnections.length < this.config.connectionLimit) {
27-
var self = this;
28-
var connection = this._createConnection();
24+
connection = this._freeConnections.shift();
25+
26+
return cb(null, connection);
27+
}
28+
29+
if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
30+
connection = this._createConnection();
31+
2932
this._allConnections.push(connection);
30-
connection.connect(function(err) {
31-
if (self._closed) {
32-
cb(new Error('Pool is closed.'));
33+
34+
return connection.connect(function(err) {
35+
if (this._closed) {
36+
return cb(new Error('Pool is closed.'));
3337
}
34-
else if (err) {
35-
cb(err);
36-
} else {
37-
cb(null, connection);
38+
if (err) {
39+
return cb(err);
3840
}
39-
});
40-
} else if (this.config.waitForConnections) {
41-
this._connectionQueue.push(cb);
42-
} else {
43-
cb(new Error('No connections available.'));
41+
42+
return cb(null, connection);
43+
}.bind(this));
44+
}
45+
46+
if (!this.config.waitForConnections) {
47+
return cb(new Error('No connections available.'));
4448
}
49+
50+
this._connectionQueue.push(cb);
4551
};
4652

47-
Pool.prototype.releaseConnection = function(connection) {
53+
Pool.prototype.releaseConnection = function (connection) {
54+
var cb;
55+
4856
if (connection._poolRemoved) {
4957
// The connection has been removed from the pool and is no longer good.
5058
if (this._connectionQueue.length) {
51-
var cb = this._connectionQueue[0];
52-
this._connectionQueue.shift();
59+
cb = this._connectionQueue.shift();
60+
5361
process.nextTick(this.getConnection.bind(this, cb));
5462
}
5563
} else if (this._connectionQueue.length) {
56-
var cb = this._connectionQueue[0];
57-
this._connectionQueue.shift();
64+
cb = this._connectionQueue.shift();
65+
5866
process.nextTick(cb.bind(null, null, connection));
5967
} else {
6068
this._freeConnections.push(connection);
6169
}
6270
};
6371

64-
Pool.prototype.end = function(cb) {
72+
Pool.prototype.end = function (cb) {
6573
this._closed = true;
66-
cb = cb || function(err) { if( err ) throw err; };
67-
var self = this;
68-
var closedConnections = 0;
74+
75+
if (typeof cb != "function") {
76+
cb = function (err) {
77+
if (err) throw err;
78+
};
79+
}
80+
6981
var calledBack = false;
82+
var closedConnections = 0;
83+
var connection;
84+
7085
var endCB = function(err) {
7186
if (calledBack) {
7287
return;
73-
} else if (err) {
74-
calledBack = true;
75-
delete endCB;
76-
cb(err);
77-
} else if (++closedConnections >= self._allConnections.length) {
88+
}
89+
90+
if (err || ++closedConnections >= this._allConnections.length) {
7891
calledBack = true;
7992
delete endCB;
80-
cb();
93+
return cb(err);
8194
}
82-
};
95+
}.bind(this);
8396

84-
if (this._allConnections.length == 0) {
85-
endCB();
86-
return;
97+
if (this._allConnections.length === 0) {
98+
return endCB();
8799
}
88100

89-
for (var i = 0; i < this._allConnections.length; ++i) {
90-
var connection = this._allConnections[i];
101+
for (var i = 0; i < this._allConnections.length; i++) {
102+
connection = this._allConnections[i];
103+
91104
connection.destroy = connection._realDestroy;
92105
connection.end = connection._realEnd;
93106
connection.end(endCB);
@@ -98,7 +111,7 @@ Pool.prototype._createConnection = function() {
98111
var self = this;
99112
var connection = (this.config.createConnection)
100113
? this.config.createConnection(this.config.connectionConfig)
101-
: Mysql.createConnection(this.config.connectionConfig);
114+
: mysql.createConnection(this.config.connectionConfig);
102115

103116
connection._realEnd = connection.end;
104117
connection.end = function(cb) {
@@ -136,14 +149,18 @@ Pool.prototype._handleConnectionError = function(connection) {
136149
};
137150

138151
Pool.prototype._removeConnection = function(connection) {
152+
var i;
153+
139154
connection._poolRemoved = true;
140-
for (var i = 0; i < this._allConnections.length; ++i) {
155+
156+
for (i = 0; i < this._allConnections.length; i++) {
141157
if (this._allConnections[i] === connection) {
142158
this._allConnections.splice(i, 1);
143159
break;
144160
}
145161
}
146-
for (var i = 0; i < this._freeConnections.length; ++i) {
162+
163+
for (i = 0; i < this._freeConnections.length; i++) {
147164
if (this._freeConnections[i] === connection) {
148165
this._freeConnections.splice(i, 1);
149166
break;
@@ -152,5 +169,6 @@ Pool.prototype._removeConnection = function(connection) {
152169

153170
connection.end = connection._realEnd;
154171
connection.destroy = connection._realDestroy;
172+
155173
this.releaseConnection(connection);
156174
};

0 commit comments

Comments
 (0)