@meteorjs/ddp-graceful-shutdown
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -43,7 +43,7 @@ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
_createClass(DDPGracefulShutdown, [{ | ||
key: 'installSIGTERMHandler', | ||
key: "installSIGTERMHandler", | ||
value: function installSIGTERMHandler() { | ||
var _this2 = this; | ||
process.on('SIGTERM', function () { | ||
process.on("SIGTERM", function () { | ||
_this2.closeConnections({ log: true }); | ||
@@ -60,3 +60,3 @@ }); | ||
}, { | ||
key: 'closeConnections', | ||
key: "closeConnections", | ||
value: function closeConnections() { | ||
@@ -67,9 +67,17 @@ var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
if (log) { | ||
console.log('Got SIGTERM; will close ' + this.connections.size + ' connection(s) in ' + this.gracePeriodMillis + 'ms'); | ||
console.log("Got SIGTERM; will close " + this.connections.size + " connection(s) in " + this.gracePeriodMillis + "ms"); | ||
} | ||
var delay = this.gracePeriodMillis / this.connections.size; | ||
this.closeOneConnectionAndScheduleNext(delay); | ||
var delay = void 0, | ||
batchSize = void 0; | ||
if (this.gracePeriodMillis >= this.connections.size) { | ||
delay = this.gracePeriodMillis / this.connections.size; | ||
batchSize = 1; | ||
} else { | ||
delay = 1; | ||
batchSize = this.connections.size / this.gracePeriodMillis; | ||
} | ||
this.closeOneBatchAndScheduleNext(delay, batchSize); | ||
} | ||
// Internal function which closes an arbitrary connection and waits to close | ||
// the next one. | ||
// Internal function which closes one batch of arbitrarily chosen connections | ||
// and waits to close the next one. | ||
// | ||
@@ -80,8 +88,26 @@ // conn.close needs to be called from within a Fiber, so this arranges to get | ||
}, { | ||
key: 'closeOneConnectionAndScheduleNext', | ||
value: function closeOneConnectionAndScheduleNext(delay) { | ||
key: "closeOneBatchAndScheduleNext", | ||
value: function closeOneBatchAndScheduleNext(delay, batchSize) { | ||
var _this3 = this; | ||
Promise.resolve().then(function () { | ||
var _connections$entries$ = _this3.connections.entries().next(), | ||
var closed = 0; | ||
while (_this3.connections.size > 0 && closed < batchSize) { | ||
_this3.closeOneConnection(); | ||
closed++; | ||
} | ||
if (_this3.connections.size > 0) { | ||
setTimeout(function () { | ||
return _this3.closeOneBatchAndScheduleNext(delay, batchSize); | ||
}, delay); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "closeOneConnection", | ||
value: function closeOneConnection() { | ||
var _this4 = this; | ||
Promise.resolve().then(function () { | ||
var _connections$entries$ = _this4.connections.entries().next(), | ||
done = _connections$entries$.done, | ||
@@ -98,9 +124,4 @@ value = _connections$entries$.value; | ||
_this3.connections.delete(id); | ||
_this4.connections.delete(id); | ||
conn.close(); | ||
if (_this3.connections.size > 0) { | ||
setTimeout(function () { | ||
return _this3.closeOneConnectionAndScheduleNext(delay); | ||
}, delay); | ||
} | ||
}); | ||
@@ -107,0 +128,0 @@ } |
{ | ||
"name": "@meteorjs/ddp-graceful-shutdown", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "Close DDP connections gradually on server shutdown", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha --require babel-register", | ||
"test": "eslint 'src/**/*.js' 'test/**/*.js' && mocha --require babel-register", | ||
"prepare": "babel src/ --out-dir lib/" | ||
@@ -28,3 +28,7 @@ }, | ||
"babel-register": "^6.26.0", | ||
"mocha": "^4.0.1" | ||
"eslint": "^4.10.0", | ||
"eslint-config-prettier": "^2.7.0", | ||
"eslint-plugin-prettier": "^2.3.1", | ||
"mocha": "^4.0.1", | ||
"prettier": "1.8.2" | ||
}, | ||
@@ -31,0 +35,0 @@ "babel": { |
@@ -11,6 +11,6 @@ // DDPGracefulShutdown is a class which tracks open connections in a DDP server | ||
// process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS. | ||
constructor({gracePeriodMillis, server}) { | ||
constructor({ gracePeriodMillis, server }) { | ||
this.gracePeriodMillis = gracePeriodMillis; | ||
this.connections = new Map; | ||
server.onConnection((conn) => { | ||
this.connections = new Map(); | ||
server.onConnection(conn => { | ||
this.connections.set(conn.id, conn); | ||
@@ -27,4 +27,4 @@ conn.onClose(() => { | ||
installSIGTERMHandler() { | ||
process.on('SIGTERM', () => { | ||
this.closeConnections({log: true}); | ||
process.on("SIGTERM", () => { | ||
this.closeConnections({ log: true }); | ||
}); | ||
@@ -38,17 +38,44 @@ } | ||
// line to stdout as well. | ||
closeConnections({log} = {}) { | ||
closeConnections({ log } = {}) { | ||
if (log) { | ||
console.log(`Got SIGTERM; will close ${ this.connections.size } connection(s) in ${ this.gracePeriodMillis }ms`); | ||
console.log( | ||
`Got SIGTERM; will close ${this.connections.size} connection(s) in ${ | ||
this.gracePeriodMillis | ||
}ms` | ||
); | ||
} | ||
const delay = this.gracePeriodMillis / this.connections.size; | ||
this.closeOneConnectionAndScheduleNext(delay); | ||
let delay, batchSize; | ||
if (this.gracePeriodMillis >= this.connections.size) { | ||
delay = this.gracePeriodMillis / this.connections.size; | ||
batchSize = 1; | ||
} else { | ||
delay = 1; | ||
batchSize = this.connections.size / this.gracePeriodMillis; | ||
} | ||
this.closeOneBatchAndScheduleNext(delay, batchSize); | ||
} | ||
// Internal function which closes an arbitrary connection and waits to close | ||
// the next one. | ||
// Internal function which closes one batch of arbitrarily chosen connections | ||
// and waits to close the next one. | ||
// | ||
// conn.close needs to be called from within a Fiber, so this arranges to get | ||
// the code in a Fiber by calling it from meteor-promise's queue. | ||
closeOneConnectionAndScheduleNext(delay) { | ||
closeOneBatchAndScheduleNext(delay, batchSize) { | ||
Promise.resolve().then(() => { | ||
const {done, value} = this.connections.entries().next(); | ||
let closed = 0; | ||
while (this.connections.size > 0 && closed < batchSize) { | ||
this.closeOneConnection(); | ||
closed++; | ||
} | ||
if (this.connections.size > 0) { | ||
setTimeout( | ||
() => this.closeOneBatchAndScheduleNext(delay, batchSize), | ||
delay | ||
); | ||
} | ||
}); | ||
} | ||
closeOneConnection() { | ||
Promise.resolve().then(() => { | ||
const { done, value } = this.connections.entries().next(); | ||
if (done) { | ||
@@ -60,5 +87,2 @@ return; | ||
conn.close(); | ||
if (this.connections.size > 0) { | ||
setTimeout(() => this.closeOneConnectionAndScheduleNext(delay), delay); | ||
} | ||
}); | ||
@@ -65,0 +89,0 @@ } |
12613
9
208
8