Skip to content

Commit

Permalink
improving error event listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jun 30, 2016
1 parent 7e75c6a commit 1eec5ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
12 changes: 11 additions & 1 deletion lib/connect.js
Expand Up @@ -20,6 +20,7 @@ function poolConnect(ctx, config) {
if (isFresh) {
$npm.utils.addReadProp(client, '$used', true, true);
}
setCtx(client, ctx);
var end = lockClientEnd(client);
resolve({
isFresh: isFresh,
Expand Down Expand Up @@ -47,6 +48,7 @@ function directConnect(ctx, config) {
});
reject(err);
} else {
setCtx(client, ctx);
var end = lockClientEnd(client);
resolve({
isFresh: true,
Expand All @@ -70,12 +72,20 @@ function lockClientEnd(client) {
// 1. the client made the call directly, against the library's documentation (invalid code)
// 2. connection with the server broke while under heavy communications, and the connection
// pool is trying to terminate all clients forcefully.
$npm.con.error("Abnormal client.end() call, due to invalid code or failed server connection.\n%s\n", $npm.utils.getLocalStack(3));
$npm.con.error("Abnormal client.end() call, due to invalid code or failed server connection.\n%s\n",
$npm.utils.getLocalStack(3));
end.call(client);
};
return end;
}

function setCtx(client, ctx) {
Object.defineProperty(client, '$ctx', {
value: ctx,
writable: true
});
}

module.exports = function (config) {
return {
pool: function (ctx) {
Expand Down
58 changes: 31 additions & 27 deletions lib/database.js
Expand Up @@ -25,24 +25,13 @@ var $arr = require('./array');
*
* For any given connection, you should only create a single {@link Database} object in a separate module,
* to be shared in your application (see the code example below). If instead you keep creating the {@link Database}
* object dynamically, your application will suffer from loss in performance and memory leaks because of event
* handlers that each {@link Database} object needs to set up.
* object dynamically, your application will suffer from loss in performance, and will be getting a warning in a
* development environment (when `NODE_ENV` = `development`):
*
* **Starting with v.4.7.0:**
* `WARNING: Creating a duplicate database object for the same connection.`
*
* If you create more than one {@link Database} object for the same connection, you will see the following warning
* in a development environment (when `NODE_ENV` = `development`):
*
* `WARNING: Creating a duplicate database object for the same connection`.
*
* And since every {@link Database} object needs to set up its own event listeners, you are likely to see the following
* warning in the console also:
*
* `Warning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit`.
*
* If you ever see any of those warnings, do not try increasing the limit, this won't help you.
* Instead, rectify your {@link Database} object initialization, so there is only one object per connection details.
* See the example provided below.
* If you ever see this warning, rectify your {@link Database} object initialization, so there is only one object
* per connection details. See the example provided below.
*
* See also: property `noWarnings` in {@link module:pg-promise Initialization Options}.
*
Expand Down Expand Up @@ -105,7 +94,7 @@ var $arr = require('./array');
function Database(cn, dc, config) {

checkForDuplicates(cn, config);
setErrorHandler(cn, dc, config);
setErrorHandler(config);

var $p = config.promise;

Expand Down Expand Up @@ -1209,7 +1198,7 @@ function Database(cn, dc, config) {

}

var dbObjects = {};
var jsHandled, nativeHandled, dbObjects = {};

function checkForDuplicates(cn, config) {
var cnKey = JSON.stringify(cn);
Expand All @@ -1223,15 +1212,30 @@ function checkForDuplicates(cn, config) {
}
}

function setErrorHandler(cn, dc, config) {
// this event only happens when the connection is lost physically,
// which cannot be tested automatically; removing from coverage:
// istanbul ignore next
config.pgp.pg.on('error', function (err) {
$npm.events.error(config.options, err, {
cn: $npm.utils.getSafeConnection(cn),
dc: dc
});
function setErrorHandler(config) {
// we do not do code coverage specific to Native Bindings:
// istanbul ignore if
if (config.options.pgNative) {
if (!nativeHandled) {
config.pgp.pg.on('error', onError);
nativeHandled = true;
}
} else {
if (!jsHandled) {
config.pgp.pg.on('error', onError);
jsHandled = true;
}
}
}

// this event only happens when the connection is lost physically,
// which cannot be tested automatically; removing from coverage:
// istanbul ignore next
function onError(err, client) {
var ctx = client.$ctx;
$npm.events.error(ctx.options, err, {
cn: $npm.utils.getSafeConnection(ctx.cn),
dc: ctx.dc
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/formatting.js
Expand Up @@ -638,7 +638,7 @@ var $as = {
* or property name that's missing within the formatting parameters.
*
* It can also be set to a function, to be called with two parameters that depend on the type of formatting being used,
* and to return the actual value:
* and to return the actual default value:
*
* - Named Parameters formatting:
* - `name` - name of the property missing in the formatting object
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "5.0.5",
"version": "5.1.0",
"description": "Promises interface for PostgreSQL",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 1eec5ac

Please sign in to comment.