NTCP: More now() calls out of loops

This commit is contained in:
zzz
2018-12-20 10:58:30 +00:00
parent 8a0602732d
commit 021375b5d5
4 changed files with 27 additions and 10 deletions

View File

@ -288,6 +288,7 @@ public class PeerHelper extends HelperBase {
" </tr>\n"); " </tr>\n");
out.write(buf.toString()); out.write(buf.toString());
buf.setLength(0); buf.setLength(0);
long now = _context.clock().now();
for (NTCPConnection con : peers) { for (NTCPConnection con : peers) {
buf.append("<tr><td class=\"cells\" align=\"left\" nowrap>"); buf.append("<tr><td class=\"cells\" align=\"left\" nowrap>");
buf.append(_context.commSystem().renderPeerHTML(con.getRemotePeer().calculateHash())); buf.append(_context.commSystem().renderPeerHTML(con.getRemotePeer().calculateHash()));
@ -306,10 +307,10 @@ public class PeerHelper extends HelperBase {
buf.append(""); buf.append("");
buf.append("</td><td class=\"cells peeripv6\" align=\"center\">").append(con.getVersion()); buf.append("</td><td class=\"cells peeripv6\" align=\"center\">").append(con.getVersion());
buf.append("</td><td class=\"cells\" align=\"center\"><span class=\"right\">"); buf.append("</td><td class=\"cells\" align=\"center\"><span class=\"right\">");
buf.append(DataHelper.formatDuration2(con.getTimeSinceReceive())); buf.append(DataHelper.formatDuration2(con.getTimeSinceReceive(now)));
buf.append("</span>").append(THINSP).append("<span class=\"left\">").append(DataHelper.formatDuration2(con.getTimeSinceSend())); buf.append("</span>").append(THINSP).append("<span class=\"left\">").append(DataHelper.formatDuration2(con.getTimeSinceSend(now)));
buf.append("</span></td><td class=\"cells\" align=\"center\"><span class=\"right\">"); buf.append("</span></td><td class=\"cells\" align=\"center\"><span class=\"right\">");
if (con.getTimeSinceReceive() < 2*60*1000) { if (con.getTimeSinceReceive(now) < 2*60*1000) {
float r = con.getRecvRate(); float r = con.getRecvRate();
buf.append(formatRate(r / 1000)); buf.append(formatRate(r / 1000));
bpsRecv += r; bpsRecv += r;
@ -317,7 +318,7 @@ public class PeerHelper extends HelperBase {
buf.append(formatRate(0)); buf.append(formatRate(0));
} }
buf.append("</span>").append(THINSP).append("<span class=\"left\">"); buf.append("</span>").append(THINSP).append("<span class=\"left\">");
if (con.getTimeSinceSend() < 2*60*1000) { if (con.getTimeSinceSend(now) < 2*60*1000) {
float r = con.getSendRate(); float r = con.getSendRate();
buf.append(formatRate(r / 1000)); buf.append(formatRate(r / 1000));
bpsSend += r; bpsSend += r;

View File

@ -262,7 +262,7 @@ class EventPumper implements Runnable {
*/ */
if ((!key.isValid()) && if ((!key.isValid()) &&
(!((SocketChannel)key.channel()).isConnectionPending()) && (!((SocketChannel)key.channel()).isConnectionPending()) &&
con.getTimeSinceCreated() > 2 * NTCPTransport.ESTABLISH_TIMEOUT) { con.getTimeSinceCreated(now) > 2 * NTCPTransport.ESTABLISH_TIMEOUT) {
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Removing invalid key for " + con); _log.info("Removing invalid key for " + con);
// this will cancel the key, and it will then be removed from the keyset // this will cancel the key, and it will then be removed from the keyset
@ -293,8 +293,8 @@ class EventPumper implements Runnable {
expire = _expireIdleWriteTime; expire = _expireIdleWriteTime;
} }
if ( con.getTimeSinceSend() > expire && if ( con.getTimeSinceSend(now) > expire &&
con.getTimeSinceReceive() > expire) { con.getTimeSinceReceive(now) > expire) {
// we haven't sent or received anything in a really long time, so lets just close 'er up // we haven't sent or received anything in a really long time, so lets just close 'er up
con.sendTerminationAndClose(); con.sendTerminationAndClose();
if (_log.shouldInfo()) if (_log.shouldInfo())

View File

@ -416,16 +416,30 @@ public class NTCPConnection implements Closeable {
/** @return milliseconds */ /** @return milliseconds */
public long getTimeSinceSend() { return _context.clock().now()-_lastSendTime; } public long getTimeSinceSend() { return _context.clock().now()-_lastSendTime; }
/**
* @return milliseconds
* @since 0.9.38
*/
public long getTimeSinceSend(long now) { return now - _lastSendTime; } public long getTimeSinceSend(long now) { return now - _lastSendTime; }
/** @return milliseconds */ /** @return milliseconds */
public long getTimeSinceReceive() { return _context.clock().now()-_lastReceiveTime; } public long getTimeSinceReceive() { return _context.clock().now()-_lastReceiveTime; }
/**
* @return milliseconds
* @since 0.9.38
*/
public long getTimeSinceReceive(long now) { return now - _lastReceiveTime; } public long getTimeSinceReceive(long now) { return now - _lastReceiveTime; }
/** @return milliseconds */ /** @return milliseconds */
public long getTimeSinceCreated() { return _context.clock().now()-_created; } public long getTimeSinceCreated() { return _context.clock().now()-_created; }
/**
* @return milliseconds
* @since 0.9.38
*/
public long getTimeSinceCreated(long now) { return now -_created; }
/** /**
* @return when this connection was created (not established) * @return when this connection was created (not established)
* @since 0.9.20 * @since 0.9.20

View File

@ -707,7 +707,7 @@ public class NTCPTransport extends TransportImpl {
* As of 0.9.20, actually returns active peer count, not total. * As of 0.9.20, actually returns active peer count, not total.
*/ */
public int countActivePeers() { public int countActivePeers() {
final long now = _context.clock().now(); final long now = _context.clock().now();
int active = 0; int active = 0;
for (NTCPConnection con : _conByIdent.values()) { for (NTCPConnection con : _conByIdent.values()) {
// con initializes times at construction, // con initializes times at construction,
@ -724,11 +724,12 @@ public class NTCPTransport extends TransportImpl {
* How many peers are we actively sending messages to (this minute) * How many peers are we actively sending messages to (this minute)
*/ */
public int countActiveSendPeers() { public int countActiveSendPeers() {
final long now = _context.clock().now();
int active = 0; int active = 0;
for (NTCPConnection con : _conByIdent.values()) { for (NTCPConnection con : _conByIdent.values()) {
// con initializes times at construction, // con initializes times at construction,
// so check message count also // so check message count also
if (con.getMessagesSent() > 0 && con.getTimeSinceSend() <= 60*1000) { if (con.getMessagesSent() > 0 && con.getTimeSinceSend(now) <= 60*1000) {
active++; active++;
} }
} }
@ -1104,12 +1105,13 @@ public class NTCPTransport extends TransportImpl {
*/ */
void expireTimedOut() { void expireTimedOut() {
int expired = 0; int expired = 0;
final long now = _context.clock().now();
for (Iterator<NTCPConnection> iter = _establishing.iterator(); iter.hasNext(); ) { for (Iterator<NTCPConnection> iter = _establishing.iterator(); iter.hasNext(); ) {
NTCPConnection con = iter.next(); NTCPConnection con = iter.next();
if (con.isClosed() || con.isEstablished()) { if (con.isClosed() || con.isEstablished()) {
iter.remove(); iter.remove();
} else if (con.getTimeSinceCreated() > ESTABLISH_TIMEOUT) { } else if (con.getTimeSinceCreated(now) > ESTABLISH_TIMEOUT) {
iter.remove(); iter.remove();
con.close(); con.close();
expired++; expired++;