Skip to content

Commit

Permalink
Keep track of the type of requests/errors encountered on NNTP connect…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
animetosho committed Jan 21, 2023
1 parent 80b2f15 commit af78ba5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cli/progressmgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var writeState = function(uploader, startTime, conn, debug) {
conn.write([
' State: ' + c.getCurrentActivity() + (c.lastActivity ? ' for ' + ((now - c.lastActivity)/1000) + 's' : ''),
' Transfer: ' + cliUtil.friendlySize(c.bytesRecv) + ' down / ' + cliUtil.friendlySize(c.bytesSent) + ' up',
' Requests: ' + c.numRequests + ' (' + c.numPosts + ' posts)',
' Requests: ' + c.numRequestsDetail(),
' Connects: ' + c.numConnects,
' Errors: ' + c.numErrors,
' Errors: ' + c.numErrorsDetail(),
'', ''
].join('\r\n'));
} else {
Expand Down
59 changes: 48 additions & 11 deletions lib/nntp.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ function NNTP(opts, rawSpeedTracker) {
this._closeWaitCb = []; // callbacks for ._end
this._requests = [];
this.rawSpeedTracker = rawSpeedTracker;
this.numErrors = {};
this.numRequests = {};

this._boundOnConnectFail = this._onConnectFail.bind(this);
this._boundOnClose = this._onClose.bind(this);
Expand Down Expand Up @@ -152,9 +154,8 @@ NNTP.prototype = {
bytesRecv: 0,
bytesSent: 0,
numConnects: 0,
numErrors: 0,
numRequests: 0,
numPosts: 0,
numErrors: null,
numRequests: null,
lastActivity: 0,
// for tracking raw upload speed
rawSpeedTracker: null,
Expand Down Expand Up @@ -301,7 +302,7 @@ NNTP.prototype = {
var cb = this._connectCb;
this._connectCb = null;
if(this._finished) return cb(err); // if already finished, this was called due to being destroyed
this.numErrors++;
this._errorStat(err ? err.code : 'connection_lost');
this._clearTimer(); // clear connection timeout timer
var errMsg = err ? err.message : 'connection lost';
if(this._connectRetries-- == 0)
Expand Down Expand Up @@ -397,7 +398,7 @@ NNTP.prototype = {
this.debug('NNTP connection lost');
}

this.numErrors++;
this._errorStat(err && err.code ? err.code : 'connection_lost');
this._close(function() {
reqsForEach(function(req, i, postTimeoutHack) {
var postTimeoutAction = postTimeoutActions[i];
Expand Down Expand Up @@ -527,10 +528,10 @@ NNTP.prototype = {
}
} else if(m) {
this.warn('Unexpected response received: ' + line.trim());
this.numErrors++;
this._errorStat('unexpected_response');
} else {
this.warn('Unexpected invalid data received: ' + line);
this.numErrors++;
this._errorStat('unexpected_data');
}
} while((p = data.indexOf('\r\n')) >= 0);
if(data.length) {
Expand Down Expand Up @@ -724,7 +725,7 @@ NNTP.prototype = {
_triggerError: function(code, desc) {
if(this._finished) {
// should only be possible to reach here if .end was called with a pending request
this.numErrors++;
this._errorStat(code);
this.warn('NNTP connection error occurred: ' + desc);
if(this._requests.length) { // is it even possible for this to be false?
var err = new NNTPError(code, desc);
Expand Down Expand Up @@ -794,7 +795,6 @@ NNTP.prototype = {
if(!this.canPost) return cb(new NNTPError('posting_denied', 'Server has indicated that posting is not allowed'));
var self = this;
(function doPost() {
self.numPosts++;
req.func = '__doRequest';
req.errVal = null;
req.type = 'post';
Expand Down Expand Up @@ -832,7 +832,7 @@ NNTP.prototype = {
if(FAILED_POST_CODES[self.postMethod].indexOf(code) > -1) {
if(req.postRetries++ < self.opts.postRetries) {
self.warn('Got "' + (code + ' ' + info).trim() + '" response when posting article ' + msgId + '; will retry (attempt ' + req.postRetries + '/' + self.opts.postRetries + ')');
self.numErrors++;
self._errorStat('post_denied');
return self._setTimer('repost', doPost, self.opts.postRetryDelay);
}
return cb(new NNTPError('post_denied', 'Server could not accept post '+ msgId + ', returned: ' + code + ' ' + info), msgId);
Expand Down Expand Up @@ -880,7 +880,7 @@ NNTP.prototype = {
}
this._startRequest(req);
if(this.state == 'connected') {
this.numRequests++;
this._reqStat(req.type);
req.cb = function() {
this._endRequest(req);
req.cbParent.apply(null, arguments);
Expand Down Expand Up @@ -1047,6 +1047,43 @@ NNTP.prototype = {
this.reqBytesSent += req.size;
if(this.rawSpeedTracker) this.rawSpeedTracker.end(this.lastActivity);
}
},
_errorStat: function(type) {
this.numErrors[type] = 1 + (this.numErrors[type] || 0);
},
_reqStat: function(type) {
this.numRequests[type] = 1 + (this.numRequests[type] || 0);
},
numErrorsDetail: function() {
var ret = [];
for(var k in this.numErrors) {
ret.push(k + ' (' + this.numErrors[k] + ')');
}
if(ret.length) return ret.join(', ');
return '-';
},
numRequestsDetail: function() {
var active = {};
this._requests.forEach(function(req) {
var type = req.type;
if(type == 'post-upload') type = 'post';
active[type] = 1 + (active[type] || 0);
});

var ret = [];
for(var k in this.numRequests) {
var count = '' + this.numRequests[k];
if(active[k]) {
if(active[k] > 3)
count += ' [*' + active[k] + ']';
else {
count += '**********'.substr(0, active[k]);
}
}
ret.push(k + ' (' + count + ')');
}
if(ret.length) return ret.join(', ');
return '-';
}
};

Expand Down

0 comments on commit af78ba5

Please sign in to comment.