-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionPool.js
30 lines (23 loc) · 918 Bytes
/
connectionPool.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
var utils = require('./utilities.js');
var connectionPool = function connectionPool() {
var connections = [];
this.add = function add(response, since) {
connections.push({ response: response, since: since, timestamp: new Date() });
};
this.endAll = function endAll(callback) {
while(connections.length) {
var connection = connections.shift();
callback(connection.response, connection.since);
}
};
var endStaleConnections = function endStaleConnection() {
while(connections.length && isStale(connections[0]))
utils.jsonResponse([], connections.shift().response);
};
var isStale = function isStale(connection) {
var now = new Date();
return now - connection.timestamp > 15000;
};
setInterval(endStaleConnections, 15000);
};
module.exports = connectionPool;