Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 1586459

Browse files
committed
wrap connect and disconnect in timeouts
1 parent b3e5c97 commit 1586459

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

initializers/transaction.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
/*jslint unparam: true */
1010

11+
var CONN_TIMEOUT = 5000;
12+
var DISCONN_TIMEOUT = 5000;
13+
1114
var handleConnectionFailure = function (api, connection, actionTemplate, error, next) {
1215
api.log("Close all opened connections", "debug");
1316
var connectionClosedCount = 0;
@@ -57,6 +60,13 @@ exports.transaction = function (api, next) {
5760
transactionPreProcessor = function (connection, actionTemplate, next) {
5861
if (actionTemplate.transaction === "read" || actionTemplate.transaction === "write") {
5962
var dbConnectionMap = {}, dbConnection, callback, connectionOpenedCount = 0;
63+
64+
var connectTimeout = function() {
65+
api.log("Timed out without obtaining all DB connections", "error");
66+
handleConnectionFailure(api, connection, actionTemplate, "Open Timeout", next);
67+
}
68+
69+
var clearMe = setTimeout(connectTimeout, CONN_TIMEOUT);
6070

6171
actionTemplate.databases.forEach(function (databaseName) {
6272
dbConnection = api.dataAccess.createConnection(databaseName);
@@ -68,12 +78,14 @@ exports.transaction = function (api, next) {
6878
callback = function (err, result) {
6979
connection.dbConnectionMap = dbConnectionMap;
7080
if (err) {
81+
clearTimeout(clearMe);
7182
handleConnectionFailure(api, connection, actionTemplate, err, next);
7283
return;
7384
}
7485

7586
connectionOpenedCount += 1;
7687
if (connectionOpenedCount === actionTemplate.databases.length) {
88+
clearTimeout(clearMe);
7789
api.log("All connections are opened", "debug");
7890
next(connection, true);
7991
}
@@ -121,6 +133,14 @@ exports.transaction = function (api, next) {
121133
* @param {Function} next - The callback function
122134
*/
123135
transactionPostProcessor = function (connection, actionTemplate, toRender, next) {
136+
137+
var disconnectTimeout = function() {
138+
api.error("Timed out without closing all DB connections", "error");
139+
// I dont want to call next(connection); here because I want to allow the execution to to continue in case connection can be closed after timeout
140+
}
141+
142+
var clearMe = setTimeout(disconnectTimeout, DISCONN_TIMEOUT);
143+
124144
var connectionClosedCount = 0;
125145
if (connection.dbConnectionMap !== null && connection.dbConnectionMap !== undefined && actionTemplate.transaction !== null && actionTemplate.transaction !== undefined) {
126146
actionTemplate.databases.forEach(function (databaseName) {
@@ -129,13 +149,15 @@ exports.transaction = function (api, next) {
129149
connection.dbConnectionMap[databaseName].disconnect();
130150
api.log("Connection is closed", "debug");
131151
if (err) {
152+
clearTimeout(clearMe);
132153
connection.error = err;
133154
next(connection);
134155
return;
135156
}
136157

137158
connectionClosedCount += 1;
138159
if (connectionClosedCount === actionTemplate.databases.length) {
160+
clearTimeout(clearMe);
139161
api.log("All connections are closed", "debug");
140162
next(connection);
141163
}

0 commit comments

Comments
 (0)