Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/nssocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var NsSocket = exports.NsSocket = function (socket, options) {
//
this._reconnect = options.reconnect || false;
this.retry = {
closed: true,
retries: 0,
max: options.maxRetries || 10,
interval: options.retryInterval || 5000,
Expand Down Expand Up @@ -205,6 +206,7 @@ NsSocket.prototype.setIdle = function setIdle(time) {
// #### forcibly destroys this nsSocket, unregister socket, remove all callbacks
//
NsSocket.prototype.destroy = function destroy() {
this.retry.closed = true; // Disable reconnection logic
if (this.socket) {
try {
this.socket.end(); // send FIN
Expand All @@ -229,6 +231,7 @@ NsSocket.prototype.destroy = function destroy() {
//
NsSocket.prototype.end = function end() {
this.connected = false;
this.retry.closed = true; // Disable reconnection logic

if (this.socket) {
try {
Expand Down Expand Up @@ -279,6 +282,8 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) {
this.host = this.host || '127.0.0.1';
args = this.port ? [this.port, this.host] : [this.host];

this.retry.closed = false;

if (callback) {
args.push(callback);
}
Expand Down Expand Up @@ -309,6 +314,9 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) {
NsSocket.prototype.reconnect = function reconnect() {
var self = this;

// Do not try to reconnect when user requested disconnect
if(self.retry.closed) return;

//
// Helper function containing the core reconnect logic
//
Expand Down