From 8bb5014d419f77f53d6d127a498dfe80691f7b0e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 2 Aug 2018 02:28:26 -0500 Subject: [PATCH] work on peer cleanup --- onionr/communicator2.py | 7 ++++++- onionr/core.py | 5 +++-- onionr/onionrpeers.py | 7 +++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index cd7c3df1..0bc6e59d 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -85,7 +85,7 @@ class OnionrCommunicatorDaemon: OnionrCommunicatorTimers(self, self.clearOfflinePeer, 58) OnionrCommunicatorTimers(self, self.lookupKeys, 60, requiresPeer=True) OnionrCommunicatorTimers(self, self.lookupAdders, 60, requiresPeer=True) - cleanupTimer = OnionrCommunicatorTimers(self, onionrpeers.peerCleanup, 300) + cleanupTimer = OnionrCommunicatorTimers(self, self.peerCleanup, 300) # set loop to execute instantly to load up peer pool (replaced old pool init wait) peerPoolTimer.count = (peerPoolTimer.frequency - 1) @@ -315,6 +315,11 @@ class OnionrCommunicatorDaemon: logger.debug('Failed to connect to ' + address) return retData + def peerCleanup(self): + '''This just calls onionrpeers.cleanupPeers, which removes dead or bad peers (offline too long, too slow)''' + onionrpeers.peerCleanup(self._core) + self.decrementThreadCount('getOnlinePeers') + def printOnlinePeers(self): '''logs online peer list''' if len(self.onlinePeers) == 0: diff --git a/onionr/core.py b/onionr/core.py index 8f01e9ca..c27596df 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -199,7 +199,8 @@ class Core: DBHash text, powValue text, failure int, - lastConnect int + lastConnect int, + lastConnectAttempt int ); ''') conn.commit() @@ -573,7 +574,7 @@ class Core: c = conn.cursor() command = (data, address) # TODO: validate key on whitelist - if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'DBHash', 'failure', 'lastConnect'): + if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'DBHash', 'failure', 'lastConnect', 'lastConnectAttempt'): raise Exception("Got invalid database key when setting address info") else: c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command) diff --git a/onionr/onionrpeers.py b/onionr/onionrpeers.py index 5488e748..73737cf9 100644 --- a/onionr/onionrpeers.py +++ b/onionr/onionrpeers.py @@ -71,6 +71,9 @@ def getScoreSortedPeerList(coreInst): peerList = sorted(peerScores, key=peerScores.get, reverse=True) return peerList -def peerCleanup(): +def peerCleanup(coreInst): # TODO, remove peers that have been offline for too long - return \ No newline at end of file + if not type(coreInst is core.Core): + raise TypeError('coreInst must be instance of core.Core') + + \ No newline at end of file