From 831edf0d248ea050b59565550f9df69fc65c8fe0 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 6 Sep 2013 21:47:54 -0400 Subject: [PATCH] completed inv refactorization --- src/class_objectHashHolder.py | 3 --- src/class_sendDataThread.py | 4 ---- src/class_singleCleaner.py | 9 +++++++++ src/shared.py | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/class_objectHashHolder.py b/src/class_objectHashHolder.py index cc636a8d..9c392765 100644 --- a/src/class_objectHashHolder.py +++ b/src/class_objectHashHolder.py @@ -20,17 +20,14 @@ class objectHashHolder(threading.Thread): self.collectionOfLists[i] = [] def run(self): - print 'objectHashHolder running.' iterator = 0 while not self.shutdown: if len(self.collectionOfLists[iterator]) > 0: - print 'objectHashHolder is submitting', len(self.collectionOfLists[iterator]), 'items to the queue.' self.sendDataThreadMailbox.put((0, 'sendinv', self.collectionOfLists[iterator])) self.collectionOfLists[iterator] = [] iterator += 1 iterator %= 10 time.sleep(1) - print 'objectHashHolder shutting down.' def holdHash(self,hash): self.collectionOfLists[random.randrange(0, 10)].append(hash) diff --git a/src/class_sendDataThread.py b/src/class_sendDataThread.py index 0c8bb580..b939b06b 100644 --- a/src/class_sendDataThread.py +++ b/src/class_sendDataThread.py @@ -108,7 +108,6 @@ class sendDataThread(threading.Thread): # to our peer after waiting a random amount of time # unless we have a long list of messages in our queue # to send. - random.seed() time.sleep(random.randrange(0, 10)) self.sock.sendall(data) self.lastTimeISentData = int(time.time()) @@ -130,7 +129,6 @@ class sendDataThread(threading.Thread): if hash not in self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware: payload += hash if payload != '': - print 'within sendinv, payload contains', len(payload)/32, 'hashes.' payload = encodeVarint(len(payload)/32) + payload headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. headerData += 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' @@ -149,8 +147,6 @@ class sendDataThread(threading.Thread): shared.sendDataQueues.remove(self.mailbox) print 'sendDataThread thread (ID:', str(id(self)) + ') ending now. Was connected to', self.peer break - else: - print '(within sendinv) payload was empty. Not sending anything' #testing. elif command == 'pong': self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware.clear() # To save memory, let us clear this data structure from time to time. As its function is to help us keep from sending inv messages to peers which sent us the same inv message mere seconds earlier, it will be fine to clear this data structure from time to time. if self.lastTimeISentData < (int(time.time()) - 298): diff --git a/src/class_singleCleaner.py b/src/class_singleCleaner.py index 3cc80868..07d56424 100644 --- a/src/class_singleCleaner.py +++ b/src/class_singleCleaner.py @@ -7,6 +7,7 @@ from helper_sql import * '''The singleCleaner class is a timer-driven thread that cleans data structures to free memory, resends messages when a remote node doesn't respond, and sends pong messages to keep connections alive if the network isn't busy. It cleans these data structures in memory: inventory (moves data to the on-disk sql database) + inventorySets (clears then reloads data out of sql database) It cleans these tables on the disk: inventory (clears data more than 2 days and 12 hours old) @@ -109,4 +110,12 @@ class singleCleaner(threading.Thread): shared.workerQueue.put(('sendmessage', '')) shared.UISignalQueue.put(( 'updateStatusBar', 'Doing work necessary to again attempt to deliver a message...')) + + # Let's also clear and reload shared.inventorySets to keep it from + # taking up an unnecessary amount of memory. + for streamNumber in shared.inventorySets: + shared.inventorySets[streamNumber] = set() + queryData = sqlQuery('''SELECT hash FROM inventory WHERE streamnumber=?''', streamNumber) + for row in queryData: + shared.inventorySets[streamNumber].add(row[0]) time.sleep(300) diff --git a/src/shared.py b/src/shared.py index 9339987a..0c0173a3 100644 --- a/src/shared.py +++ b/src/shared.py @@ -304,7 +304,7 @@ def doCleanShutdown(): def broadcastToSendDataQueues(data): # logger.debug('running broadcastToSendDataQueues') for q in sendDataQueues: - q.put((data)) + q.put(data) def flushInventory(): #Note that the singleCleanerThread clears out the inventory dictionary from time to time, although it only clears things that have been in the dictionary for a long time. This clears the inventory dictionary Now.