Skip to content

Commit

Permalink
fix: prevent max listeners warning
Browse files Browse the repository at this point in the history
If establishing a database connection is slow
and database migration runs and there are many
models, sql operations are queued up and this
leads to the node.js max emitters exceeded
warning.

A default value for max emitters has now
been introduced, and it can also be configured
in datasources.json.

Signed-off-by: Dominique Emond <[email protected]>
  • Loading branch information
emonddr committed Aug 1, 2019
1 parent ad4edc6 commit afdc19b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ DataSource.prototype._setupConnector = function() {
this.connector.dataSource = this;
}
const dataSource = this;

// Set max listeners to a default/configured value
// solves bug https://github.com/strongloop/loopback-next/issues/2198
dataSource.setMaxListeners(dataSource.getMaxOfflineRequests());

this.connector.log = function(query, start) {
dataSource.log(query, start);
};
Expand Down Expand Up @@ -2699,6 +2704,22 @@ DataSource.prototype.beginTransaction = function(options) {
return Transaction.begin(this.connector, options);
};

/**
* Get the maximum number of event listeners
*/
DataSource.prototype.getMaxOfflineRequests = function() {
// Set max listeners to a default value
// Override this default value with a datasource setting
// 'maxOfflineRequests' from an application's datasources.json
// solves bug https://github.com/strongloop/loopback-next/issues/2198
let maxOfflineRequests = 16;
if (this.settings && this.settings.maxOfflineRequests) {
if (this.settings.maxOfflineRequests > maxOfflineRequests)
maxOfflineRequests = this.settings.maxOfflineRequests;
}
return maxOfflineRequests;
};

/*! The hidden property call is too expensive so it is not used that much
*/
/**
Expand Down

0 comments on commit afdc19b

Please sign in to comment.