Skip to content

Commit

Permalink
Note down the cause of inactive connections
Browse files Browse the repository at this point in the history
More info for #99
  • Loading branch information
animetosho committed Jan 21, 2023
1 parent af78ba5 commit b5b90b2
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/nntp.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function NNTP(opts, rawSpeedTracker) {

NNTP.prototype = {
state: 'inactive',
stateCause: 'unused',
socket: null,
postMethod: 'POST',
_timer: null,
Expand Down Expand Up @@ -318,7 +319,7 @@ NNTP.prototype = {
this._close(next);
}.bind(this))((this._finished ?
function() {
this._setState('inactive');
this._setState('inactive', 'connect fail');
var e = new NNTPError('connect_fail', 'NNTP connection failed: ' + errMsg);
cb(e);
this._runCloseCbs();
Expand All @@ -332,7 +333,7 @@ NNTP.prototype = {
}
:
function() {
this._setState('waiting');
this._setState('waiting', 'connect fail');
this.warn('NNTP connection failed: ' + errMsg + ', reconnecting after ' +(this.opts.reconnectDelay/1000)+ ' second(s)... (attempt ' + (this.opts.connectRetries - this._connectRetries) + '/' + this.opts.connectRetries + ')');

this._connectCb = cb; // set this back for .destroy() calls to work properly (.connect() will reset this anyway)
Expand All @@ -343,7 +344,7 @@ NNTP.prototype = {
},
_onClose: function(had_error) {
if(!this.socket) return; // node 10.x issue
this._setState('disconnected');
this._setState('disconnected', 'closed');
this.dataQueue = [];
this.dataLength = 0;
this.debug('NNTP connection closed');
Expand Down Expand Up @@ -424,7 +425,7 @@ NNTP.prototype = {
self.connect();
});
} else {
self._setState('inactive');
self._setState('inactive', 'no requests');
}
});
},
Expand Down Expand Up @@ -593,7 +594,7 @@ NNTP.prototype = {
} else
self._end(cb, CMD_QUIT);
})(function() {
self._setState('inactive');
self._setState('inactive', 'end');
self._callMulti(self._getNotifyCbs(), new NNTPError('cancelled', 'Request cancelled (via \'end\')'));
self._runCloseCbs();
self._resetState();
Expand All @@ -617,7 +618,7 @@ NNTP.prototype = {
} else
cb();
})(function() {
self._setState('inactive');
self._setState('inactive', 'close');
self._callMulti(cbs, new NNTPError('cancelled', 'Request cancelled (via \'close\')'));
self._runCloseCbs();
self._resetState();
Expand All @@ -633,7 +634,7 @@ NNTP.prototype = {
if(this._throttleHandle)
this._throttleHandle.cancel();
this._destroy();
this._setState('inactive');
this._setState('inactive', 'destroy');
this._resetState();
this._callMulti(cbs, new NNTPError('cancelled', 'Request cancelled (via \'destroy\')'));
},
Expand Down Expand Up @@ -1023,16 +1024,20 @@ NNTP.prototype = {
},

getCurrentActivity: function() {
if(this.state != 'connected')
if(this.state != 'connected') {
if(this.stateCause)
return this.state + ' [' + this.stateCause + ']';
return this.state;
}
if(!this._requests.length)
return 'idle';
if(this._requests[0].type == 'post-upload')
return 'posting';
return 'requesting (' + this._requests[0].type + ')';
},
_setState: function(state) {
_setState: function(state, cause) {
this.state = state;
this.stateCause = cause;
this.lastActivity = Date.now();
},
_startRequest: function(req) {
Expand Down Expand Up @@ -1070,6 +1075,12 @@ NNTP.prototype = {
active[type] = 1 + (active[type] || 0);
});

// check for mis-labelled stuff?
for(var k in active) {
if(!(k in this.numRequests))
this.numRequests[k] = 0;
}

var ret = [];
for(var k in this.numRequests) {
var count = '' + this.numRequests[k];
Expand Down

0 comments on commit b5b90b2

Please sign in to comment.