-
Notifications
You must be signed in to change notification settings - Fork 31
/
outstandingQueries.js
47 lines (42 loc) · 1.22 KB
/
outstandingQueries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* this is a curious class
*
* Imagine this scenario:
*
* 1. we open a db connection.
* 2. run two simultaneous queries against it, i.e. Promise.all([query1(), query2()])
* 3. the first query breaks, due to an SQL exception
* 4. We log the error and close the database immediately
* 5. however, query2 is still executing and prevents the database connection from being closed
* 6. the connection is now in a broken state, it cannot be used, but remains in the conneciton pool
*
* this class ensures that we don't close the connection until all queries have finished
*/
var promiseFinally = require('./promiseFinally');
module.exports = function() {
return {
queries: 0,
execute: function(p) {
var self = this;
this.queries++;
return promiseFinally(p, function () {
self.queries--;
if (self.queries === 0 && self._whenFinished) {
self._whenFinished();
}
});
},
whenNotExecuting: function(wf) {
var self = this;
return new Promise(function (resolve) {
if (self.queries == 0) {
resolve(wf());
} else {
self._whenFinished = function() {
resolve(wf());
};
}
});
}
};
}