Skip to content

Commit

Permalink
Track and display NNTP connections' total connected/idle times
Browse files Browse the repository at this point in the history
  • Loading branch information
animetosho committed Jan 24, 2024
1 parent 175124c commit 0e57b66
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cli/progressmgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ var writeState = function(uploader, startTime, conn, debug) {
host = 'unix:' + c.opts.connect.path;
else
host = c.opts.connect.host + ':' + c.opts.connect.port;
var timeStats = c.timeStats(now);
conn.write([
' Host: ' + host,
' State: ' + c.getCurrentActivity() + (c.lastActivity ? ' for ' + ((now - c.lastActivity)/1000) + 's' : ''),
' Transfer: ' + cliUtil.friendlySize(c.bytesRecv) + ' down / ' + cliUtil.friendlySize(c.bytesSent) + ' up',
' Requests: ' + c.numRequestsDetail(),
' Connects: ' + c.numConnects,
' Time Idle: ' + (timeStats.idle / 1000) + 's (' + toPercent(timeStats.connected ?timeStats.idle/timeStats.connected : 0) + ')',
' Errors: ' + c.numErrorsDetail(),
'', ''
].join('\r\n'));
Expand Down
38 changes: 37 additions & 1 deletion lib/nntp.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ NNTP.prototype = {
numErrors: null,
numRequests: null,
lastActivity: 0,
lastConnectedStart: null,
lastIdleStart: null,
connectedTime: 0,
idleTime: 0,
// for tracking raw upload speed
rawSpeedTracker: null,
reqBytesSent: 0,
Expand Down Expand Up @@ -990,6 +994,9 @@ NNTP.prototype = {
var nr = this._requests[0];
this._requestSetTimer(nr.type, nr.ts + this.opts.timeout - Date.now());
}
if(!this._requests.length && this.state == 'connected') {
this.lastIdleStart = Date.now();
}
return req;
},
_write: function(data, rtnLen) {
Expand Down Expand Up @@ -1046,14 +1053,32 @@ NNTP.prototype = {
return 'requesting (' + this._requests[0].type + ')';
},
_setState: function(state, cause) {
this.lastActivity = Date.now();
if(state == 'connected') {
this.lastConnectedStart = this.lastActivity;
if(!this._requests.length)
this.lastIdleStart = this.lastActivity;
}
else if(this.state == 'connected') {
if(this.lastConnectedStart) // should always be set
this.connectedTime += this.lastActivity - this.lastConnectedStart;
this.lastConnectedStart = null;
if(this.lastIdleStart) {
this.idleTime += this.lastActivity - this.lastIdleStart;
this.lastIdleStart = null;
}
}
this.state = state;
this.stateCause = cause;
this.lastActivity = Date.now();
},
_startRequest: function(req) {
this._requests.push(req);
this.lastActivity = (req.ts = Date.now());
if(this.rawSpeedTracker) this.rawSpeedTracker.start(req.ts);
if(this.lastIdleStart) {
this.idleTime += this.lastActivity - this.lastIdleStart;
this.lastIdleStart = null;
}
},
_endRequest: function(req) {
this.lastActivity = Date.now();
Expand Down Expand Up @@ -1105,6 +1130,17 @@ NNTP.prototype = {
}
if(ret.length) return ret.join(', ');
return '-';
},
timeStats: function(now) {
var stats = {connected: this.connectedTime, idle: this.idleTime};
if(!now) now = Date.now();
if(this.state == 'connected') {
if(this.lastConnectedStart) // should always be set
stats.connected += now - this.lastConnectedStart;
if(this.lastIdleStart && !this._requests.length)
stats.idle += now - this.lastIdleStart;
}
return stats;
}
};

Expand Down

0 comments on commit 0e57b66

Please sign in to comment.