Get call to system timer out of tight loop when counting connections

This commit is contained in:
zab2
2018-12-19 10:51:53 +00:00
parent 20876ff307
commit 8a0602732d
2 changed files with 7 additions and 2 deletions

View File

@@ -415,9 +415,13 @@ public class NTCPConnection implements Closeable {
/** @return milliseconds */
public long getTimeSinceSend() { return _context.clock().now()-_lastSendTime; }
public long getTimeSinceSend(long now) { return now - _lastSendTime; }
/** @return milliseconds */
public long getTimeSinceReceive() { return _context.clock().now()-_lastReceiveTime; }
public long getTimeSinceReceive(long now) { return now - _lastReceiveTime; }
/** @return milliseconds */
public long getTimeSinceCreated() { return _context.clock().now()-_created; }

View File

@@ -707,12 +707,13 @@ public class NTCPTransport extends TransportImpl {
* As of 0.9.20, actually returns active peer count, not total.
*/
public int countActivePeers() {
final long now = _context.clock().now();
int active = 0;
for (NTCPConnection con : _conByIdent.values()) {
// con initializes times at construction,
// so check message count also
if ((con.getMessagesSent() > 0 && con.getTimeSinceSend() <= 5*60*1000) ||
(con.getMessagesReceived() > 0 && con.getTimeSinceReceive() <= 5*60*1000)) {
if ((con.getMessagesSent() > 0 && con.getTimeSinceSend(now) <= 5*60*1000) ||
(con.getMessagesReceived() > 0 && con.getTimeSinceReceive(now) <= 5*60*1000)) {
active++;
}
}