Update connection.js to support a "shutdown" command

This change adds a shutdown method that indicates to socketDrained that it should close connections once there are no more notifications to send. The side effect is that the node process will end. I've tested the change both with all successful messages, as well as when a few messages can't be sent. In both cases, the program does not shutdown until all messages that can be successfully sent our sent.

Signed-off-by: Andrew Naylor <argon@mkbot.net>
This commit is contained in:
ejc3@yahoo-inc.com 2013-10-30 04:24:22 -07:00 коммит произвёл Andrew Naylor
Родитель 3a3163d27b
Коммит ebcb0c92e9
1 изменённых файлов: 17 добавлений и 1 удалений

Просмотреть файл

@ -84,6 +84,9 @@ function Connection (options) {
this.socketId = 0;
// when true, we end all sockets after the pending notifications reach 0
this.shutdownPending = false;
events.EventEmitter.call(this);
}
@ -99,7 +102,7 @@ Connection.prototype.checkInitialized = function () {
};
/**
* You should never need to call this method, initialisation and connection is handled by {@link Connection#sendNotification}
* You should never need to call this method, initialization and connection is handled by {@link Connection#sendNotification}
* @private
*/
Connection.prototype.initialize = function () {
@ -362,6 +365,12 @@ Connection.prototype.socketDrained = function(socket, serviceBuffer) {
}
this.runningOnNextTick = true;
}
if (this.notificationBuffer.length === 0 && this.shutdownPending) {
debug("closing connections");
for (var i = this.sockets.length - 1; i >= 0; i--) {
this.sockets[i].end();
}
}
};
/**
@ -706,4 +715,11 @@ Connection.prototype.sendNotification = function (notification) {
return this.pushNotification(notification, notification.device);
};
/**
* End connections with APNS once we've finished sending all notifications
*/
Connection.prototype.shutdown = function () {
this.shutdownPending = true;
};
module.exports = Connection;