work on better peer connections

This commit is contained in:
Kevin Froman 2019-01-11 16:59:21 -06:00
parent d6eabe9f12
commit f82e7bfb59
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 30 additions and 6 deletions

View File

@ -143,9 +143,9 @@ class PublicAPI:
@app.route('/pex')
def peerExchange():
response = ','.join(clientAPI._core.listAdders())
response = ','.join(clientAPI._core.listAdders(recent=3600))
if len(response) == 0:
response = 'none'
response = ''
return Response(response)
@app.route('/announce', methods=['post'])

View File

@ -107,7 +107,7 @@ class OnionrCommunicatorDaemon:
netCheckTimer = OnionrCommunicatorTimers(self, self.daemonTools.netCheck, 600)
if config.get('general.security_level') == 0:
announceTimer = OnionrCommunicatorTimers(self, self.daemonTools.announceNode, 86400, requiresPeer=True, maxThreads=1)
announceTimer = OnionrCommunicatorTimers(self, self.daemonTools.announceNode, 3600, requiresPeer=True, maxThreads=1)
announceTimer.count = (announceTimer.frequency - 120)
else:
logger.debug('Will not announce node.')

View File

@ -405,7 +405,7 @@ class Core:
return
def listAdders(self, randomOrder=True, i2p=True):
def listAdders(self, randomOrder=True, i2p=True, recent=0):
'''
Return a list of addresses
'''
@ -417,8 +417,17 @@ class Core:
addresses = c.execute('SELECT * FROM adders;')
addressList = []
for i in addresses:
if len(i[0].strip()) == 0:
continue
addressList.append(i[0])
conn.close()
testList = list(addressList) # create new list to iterate
for address in testList:
try:
if recent > 0 and (self._utils.getEpoch() - self.getAddressInfo(address, 'lastConnect')) > recent:
raise TypeError # If there is no last-connected date or it was too long ago, don't add peer to list if recent is not 0
except TypeError:
addressList.remove(address)
return addressList
def listPeers(self, randomOrder=True, getPow=False, trust=0):

View File

@ -28,6 +28,7 @@ class PeerProfiles:
self.friendSigCount = 0
self.success = 0
self.failure = 0
self.connectTime = None
if not isinstance(coreInst, core.Core):
raise TypeError("coreInst must be a type of core.Core")
@ -35,6 +36,7 @@ class PeerProfiles:
assert isinstance(self.coreInst, core.Core)
self.loadScore()
self.getConnectTime()
return
def loadScore(self):
@ -44,7 +46,13 @@ class PeerProfiles:
except (TypeError, ValueError) as e:
self.success = 0
self.score = self.success
def getConnectTime(self):
try:
self.connectTime = self.coreInst.getAddressInfo(self.address, 'lastConnect')
except KeyError:
pass
def saveScore(self):
'''Save the node's score to the database'''
self.coreInst.setAddressInfo(self.address, 'success', self.score)
@ -61,14 +69,21 @@ def getScoreSortedPeerList(coreInst):
peerList = coreInst.listAdders()
peerScores = {}
peerTimes = {}
for address in peerList:
# Load peer's profiles into a list
profile = PeerProfiles(address, coreInst)
peerScores[address] = profile.score
if not isinstance(profile.connectTime, type(None)):
peerTimes[address] = profile.connectTime
else:
peerTimes[address] = 9000
# Sort peers by their score, greatest to least
# Sort peers by their score, greatest to least, and then last connected time
peerList = sorted(peerScores, key=peerScores.get, reverse=True)
peerList = sorted(peerTimes, key=peerTimes.get, reverse=True)
print(peerList)
return peerList
def peerCleanup(coreInst):