From bd5b6a1802bd190d0dc3326ddd1665872f81b681 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 6 Aug 2019 16:19:26 -0500 Subject: [PATCH] do not update address databases on every request --- onionr/communicator/__init__.py | 3 ++- onionr/communicator/peeraction.py | 5 ++-- onionr/httpapi/fdsafehandler.py | 2 +- onionr/onionrpeers/peerprofiles.py | 15 +++++++++++- onionr/onionrservices/connectionserver.py | 2 +- onionr/static-data/www/mail/mail.js | 30 ++++++++++++++++++++--- 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/onionr/communicator/__init__.py b/onionr/communicator/__init__.py index 40d6106a..15ff80a7 100755 --- a/onionr/communicator/__init__.py +++ b/onionr/communicator/__init__.py @@ -224,8 +224,9 @@ class OnionrCommunicatorDaemon: retData = i break else: - # if the peer's profile is not loaded, return a new one. connectNewPeer adds it the list on connect + # if the peer's profile is not loaded, return a new one. connectNewPeer also adds it to the list on connect retData = onionrpeers.PeerProfiles(peer) + self.peerProfiles.append(retData) return retData def getUptime(self): diff --git a/onionr/communicator/peeraction.py b/onionr/communicator/peeraction.py index fc982fe7..d693ea8e 100644 --- a/onionr/communicator/peeraction.py +++ b/onionr/communicator/peeraction.py @@ -49,6 +49,7 @@ def peer_action(comm_inst, peer, action, data='', returnHeaders=False, max_resp_ except ValueError: pass else: - keydb.transportinfo.set_address_info(peer, 'lastConnect', epoch.get_epoch()) - comm_inst.getPeerProfileInstance(peer).addScore(1) + peer_profile = comm_inst.getPeerProfileInstance(peer) + peer_profile.update_connect_time() + peer_profile.addScore(1) return retData # If returnHeaders, returns tuple of data, headers. if not, just data string diff --git a/onionr/httpapi/fdsafehandler.py b/onionr/httpapi/fdsafehandler.py index 1f50bf32..a24d78ca 100644 --- a/onionr/httpapi/fdsafehandler.py +++ b/onionr/httpapi/fdsafehandler.py @@ -7,7 +7,7 @@ class FDSafeHandler(WSGIHandler): self.timeout.start() try: WSGIHandler.handle(self) - except gevent.Timeout as ex: + except Timeout as ex: if ex is self.timeout: pass else: diff --git a/onionr/onionrpeers/peerprofiles.py b/onionr/onionrpeers/peerprofiles.py index 6d4d8252..1f1aa536 100644 --- a/onionr/onionrpeers/peerprofiles.py +++ b/onionr/onionrpeers/peerprofiles.py @@ -18,6 +18,10 @@ along with this program. If not, see . ''' from coredb import keydb +from onionrutils import epoch + +UPDATE_DELAY = 300 + class PeerProfiles: ''' PeerProfiles @@ -32,6 +36,8 @@ class PeerProfiles: self.loadScore() self.getConnectTime() + + self.last_updated = {'connect_time': UPDATE_DELAY, 'score': UPDATE_DELAY} # Last time a given value was updated return def loadScore(self): @@ -47,10 +53,17 @@ class PeerProfiles: self.connectTime = int(keydb.transportinfo.get_address_info(self.address, 'lastConnect')) except (KeyError, ValueError, TypeError) as e: pass + + def update_connect_time(self): + if epoch.get_epoch() - self.last_updated['connect_time'] >= UPDATE_DELAY: + self.last_updated['connect_time'] = epoch.get_epoch() + keydb.transportinfo.set_address_info(self.address, 'lastConnect', epoch.get_epoch()) def saveScore(self): '''Save the node's score to the database''' - keydb.transportinfo.set_address_info(self.address, 'success', self.score) + if epoch.get_epoch() - self.last_updated['score'] >= UPDATE_DELAY: + self.last_updated['score'] = epoch.get_epoch() + keydb.transportinfo.set_address_info(self.address, 'success', self.score) return def addScore(self, toAdd): diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index b7e15334..726f9179 100755 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -70,7 +70,7 @@ class ConnectionServer: try: for x in range(3): - attempt = basicrequests.do_post_request(comm_inst.onionrInst, 'http://' + address + '/bs/' + response.service_id, port=socks) + attempt = basicrequests.do_post_request('http://' + address + '/bs/' + response.service_id, port=socks) if attempt == 'success': break else: diff --git a/onionr/static-data/www/mail/mail.js b/onionr/static-data/www/mail/mail.js index d4cae20b..c4d4edb2 100755 --- a/onionr/static-data/www/mail/mail.js +++ b/onionr/static-data/www/mail/mail.js @@ -106,9 +106,12 @@ function openThread(bHash, sender, date, sigBool, pubkey, subjectLine){ function setActiveTab(tabName){ threadPart.innerHTML = "" + window.inboxActive = false switch(tabName){ case 'inbox': + window.inboxActive = true refreshPms() + getInbox() break case 'sent': getSentbox() @@ -223,9 +226,14 @@ function loadInboxEntries(bHash){ } function getInbox(){ + if (! window.inboxActive){ + return + } + var els = document.getElementsByClassName('threadEntry') var showed = false var requested = '' for(var i = 0; i < pms.length; i++) { + var add = true if (pms[i].trim().length == 0){ threadPart.innerText = 'No messages to show ¯\\_(ツ)_/¯' continue @@ -234,7 +242,14 @@ function getInbox(){ threadPlaceholder.style.display = 'none' showed = true } - loadInboxEntries(pms[i]) + for (var x = 0; x < els.length; x++){ + if (pms[i] === els[x].getAttribute('data-hash')){ + add = false + } + } + if (add && window.inboxActive) { + loadInboxEntries(pms[i]) + } } if (! showed){ threadPlaceholder.style.display = 'block' @@ -306,7 +321,10 @@ function showSentboxWindow(to, content){ overlay('sentboxDisplay') } -function refreshPms(){ +function refreshPms(callNext){ + if (! window.inboxActive){ + return + } fetch('/mail/getinbox', { headers: { "token": webpass @@ -314,7 +332,9 @@ fetch('/mail/getinbox', { .then((resp) => resp.text()) // Transform the data into json .then(function(data) { pms = data.split(',') - getInbox() + if (callNext){ + getInbox() + } }) } @@ -360,4 +380,6 @@ fetch('/friends/list', { setActiveTab('inbox') setInterval(function(){mailPing()}, 10000) -mailPing() \ No newline at end of file +mailPing() +window.inboxInterval = setInterval(function(){refreshPms(true)}, 3000) +refreshPms(true) \ No newline at end of file