From 2ed4dd4c8ae1a8facb71b7783e21d689a1ac8297 Mon Sep 17 00:00:00 2001 From: Almir Kadric Date: Sun, 10 Apr 2016 03:19:35 +0900 Subject: [PATCH] fixed a bunch of issues I found when trying to use nssocket --- lib/nssocket.js | 116 ++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/lib/nssocket.js b/lib/nssocket.js index 762e306..e1e1f62 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -205,6 +205,15 @@ NsSocket.prototype.setIdle = function setIdle(time) { // #### forcibly destroys this nsSocket, unregister socket, remove all callbacks // NsSocket.prototype.destroy = function destroy() { + this._reconnect = false; + this.connected = false; + + // Prevent reconnect on socket close + if (this.retry.timer) { + clearTimeout(this.retry.timer); + } + + // Attempt to disconnect the socket if (this.socket) { try { this.socket.end(); // send FIN @@ -228,8 +237,15 @@ NsSocket.prototype.destroy = function destroy() { // #### closes the underlying socket, recommend you call destroy after // NsSocket.prototype.end = function end() { + this._reconnect = false; this.connected = false; + // Prevent reconnect on socket close + if (this.retry.timer) { + clearTimeout(this.retry.timer); + } + + // Attempt to disconnect the socket if (this.socket) { try { this.socket.end(); @@ -241,63 +257,65 @@ NsSocket.prototype.end = function end() { this.socket = null; } - - return; }; // // ### function connect (port[, host, callback]) // A passthrough to the underlying socket's connect function // -NsSocket.prototype.connect = function connect(/*port, host, callback*/) { - var args = Array.prototype.slice.call(arguments), - self = this, - callback, - host, - port; - - args.forEach(function handle(arg) { - var type = typeof arg; - switch (type) { - case 'number': - port = arg; - break; - case 'string': - host = arg; - break; - case 'function': - callback = arg; - break; - default: - self.emit('error', new Error('bad argument to connect')); - break; +NsSocket.prototype.connect = function () { + var options = {}; + var cb; + + // Parse out arguments into options object + if (typeof arguments[0] === 'object') { + options = arguments[0]; + + if (typeof arguments[1] === 'function') { + cb = arguments[1]; + } + } else if (typeof arguments[0] === 'number' || !isNaN(parseInt(arguments[0]))) { + options.port = arguments[0]; + + if (typeof arguments[1] === 'string') { + options.host = arguments[1]; + } else if (typeof arguments[1] === 'function') { + cb = arguments[1]; } - }); - this.port = port || this.port; - this.host = host || this.host; - this.host = this.host || '127.0.0.1'; - args = this.port ? [this.port, this.host] : [this.host]; + if (typeof arguments[2] === 'function') { + cb = arguments[2]; + } + } else if (typeof arguments[0] === 'string') { + options.path = arguments[0]; + + if (typeof arguments[1] === 'function') { + cb = arguments[1]; + } + } - if (callback) { - args.push(callback); + // Check if there were any connect options, if not try and load it from cache + if (Object.keys(options).length === 0 && this.connectOptions) { + options = this.connectOptions; } + // Cache connection options for reconnect + this.connectOptions = options; + + // Ensure socket type is valid if (['tcp4', 'tls'].indexOf(this._type) === -1) { return this.emit('error', new Error('Unknown Socket Type')); } - var errHandlers = self.listeners('error'); - + // Copy the last error from nssocket onto the error event if it exists + var errHandlers = this.listeners('error'); if (errHandlers.length > 0) { - // - // copy the last error from nssocker onto the error event. - // - self.socket._events.error = errHandlers[errHandlers.length-1]; + this.socket._events.error = errHandlers[errHandlers.length-1]; } + // Initiate connection this.connected = true; - this.socket.connect.apply(this.socket, args); + this.socket.connect(options, cb); }; // @@ -306,51 +324,45 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) { // This instance will attempt to reconnect until `this.retry.max` is reached, // with an interval increasing by powers of 10. // -NsSocket.prototype.reconnect = function reconnect() { +NsSocket.prototype.reconnect = function () { var self = this; - // // Helper function containing the core reconnect logic - // function doReconnect() { - // // Cleanup and recreate the socket associated // with this instance. - // self.retry.waiting = true; self.socket.removeAllListeners(); self.socket = common.createSocket(self._options); - // // Cleanup reconnect logic once the socket connects - // self.socket.once('connect', function () { self.retry.waiting = false; self.retry.retries = 0; }); - // // Attempt to reconnect the socket - // self._setup(); self.connect(); } - // + // Helper function which attempts to retry if // it is less than the maximum - // function tryReconnect() { + self.retry.timer = null; self.retry.retries++; - if (self.retry.retries >= self.retry.max) { + if (self.retry.max !== true && self.retry.retries >= self.retry.max) { return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max)); } doReconnect(); } - this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries); - setTimeout(tryReconnect, this.retry.wait); + + // TODO: IMPLEMENT EXPONENTIAL BACK-OFF + this.retry.wait = this.retry.interval; + this.retry.timer = setTimeout(tryReconnect, this.retry.wait); }; //