More accurate PendingUpload tracking
- works correctly when starting offline - stops tracking after after 60 seconds but only if at least 1 successful upload
This commit is contained in:
parent
9263f53d86
commit
15077c9388
|
@ -187,12 +187,12 @@ class sendDataThread(threading.Thread):
|
||||||
logger.error('send pong failed')
|
logger.error('send pong failed')
|
||||||
break
|
break
|
||||||
elif command == 'sendRawData':
|
elif command == 'sendRawData':
|
||||||
hash = None
|
objectHash = None
|
||||||
if type(data) in [list, tuple]:
|
if type(data) in [list, tuple]:
|
||||||
hash, data = data
|
objectHash, data = data
|
||||||
try:
|
try:
|
||||||
self.sendBytes(data)
|
self.sendBytes(data)
|
||||||
PendingUpload().delete(hash)
|
PendingUpload().delete(objectHash)
|
||||||
except:
|
except:
|
||||||
logger.error('Sending of data to ' + str(self.peer) + ' failed. sendDataThread thread ' + str(self) + ' ending now.', exc_info=True)
|
logger.error('Sending of data to ' + str(self.peer) + ' failed. sendDataThread thread ' + str(self) + ' ending now.', exc_info=True)
|
||||||
break
|
break
|
||||||
|
|
|
@ -225,36 +225,48 @@ class PendingUpload(object):
|
||||||
# end by this time in any case
|
# end by this time in any case
|
||||||
self.deadline = 0
|
self.deadline = 0
|
||||||
self.maxLen = 0
|
self.maxLen = 0
|
||||||
|
# during shutdown, wait up to 20 seconds to finish uploading
|
||||||
|
self.shutdownWait = 20
|
||||||
|
# forget tracking objects after 60 seconds
|
||||||
|
self.objectWait = 60
|
||||||
|
# wait 10 seconds between clears
|
||||||
|
self.clearDelay = 10
|
||||||
|
self.lastCleared = time.time()
|
||||||
|
|
||||||
def add(self, objectHash = None):
|
def add(self, objectHash = None):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
# add a new object into existing thread lists
|
# add a new object into existing thread lists
|
||||||
if objectHash:
|
if objectHash:
|
||||||
if objectHash not in self.hashes:
|
if objectHash not in self.hashes:
|
||||||
self.hashes[objectHash] = []
|
self.hashes[objectHash] = {'created': time.time(), 'sendCount': 0, 'peers': []}
|
||||||
for thread in threadingEnumerate():
|
for thread in threadingEnumerate():
|
||||||
if thread.isAlive() and hasattr(thread, 'peer') and \
|
if thread.isAlive() and hasattr(thread, 'peer') and \
|
||||||
thread.peer not in self.hashes[objectHash]:
|
thread.peer not in self.hashes[objectHash]['peers']:
|
||||||
self.hashes[objectHash].append(thread.peer)
|
self.hashes[objectHash]['peers'].append(thread.peer)
|
||||||
# add all objects into the current thread
|
# add all objects into the current thread
|
||||||
else:
|
else:
|
||||||
for objectHash in self.hashes:
|
for objectHash in self.hashes:
|
||||||
if current_thread().peer not in self.hashes[objectHash]:
|
if current_thread().peer not in self.hashes[objectHash]['peers']:
|
||||||
self.hashes[objectHash].append(current_thread().peer)
|
self.hashes[objectHash]['peers'].append(current_thread().peer)
|
||||||
|
|
||||||
def len(self):
|
def len(self):
|
||||||
|
self.clearHashes()
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return sum(len(self.hashes[x]) > 0 for x in self.hashes)
|
return sum(1
|
||||||
|
for x in self.hashes if (self.hashes[x]['created'] + self.objectWait < time.time() or
|
||||||
|
self.hashes[x]['sendCount'] == 0))
|
||||||
|
|
||||||
def _progress(self):
|
def _progress(self):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return float(sum(len(self.hashes[x]) for x in self.hashes))
|
return float(sum(len(self.hashes[x]['peers'])
|
||||||
|
for x in self.hashes if (self.hashes[x]['created'] + self.objectWait < time.time()) or
|
||||||
|
self.hashes[x]['sendCount'] == 0))
|
||||||
|
|
||||||
def progress(self, throwDeadline=True):
|
def progress(self, raiseDeadline=True):
|
||||||
if self.maxLen < self._progress():
|
if self.maxLen < self._progress():
|
||||||
self.maxLen = self._progress()
|
self.maxLen = self._progress()
|
||||||
if self.deadline < time.time():
|
if self.deadline < time.time():
|
||||||
if self.deadline > 0 and throwDeadline:
|
if self.deadline > 0 and raiseDeadline:
|
||||||
raise PendingUploadDeadlineException
|
raise PendingUploadDeadlineException
|
||||||
self.deadline = time.time() + 20
|
self.deadline = time.time() + 20
|
||||||
try:
|
try:
|
||||||
|
@ -262,29 +274,48 @@ class PendingUpload(object):
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return 1.0
|
return 1.0
|
||||||
|
|
||||||
def delete(self, objectHash):
|
def clearHashes(self, objectHash=None):
|
||||||
|
if objectHash is None:
|
||||||
|
if self.lastCleared > time.time() - self.clearDelay:
|
||||||
|
return
|
||||||
|
objects = self.hashes.keys()
|
||||||
|
else:
|
||||||
|
objects = objectHash,
|
||||||
|
with self.lock:
|
||||||
|
for i in objects:
|
||||||
|
try:
|
||||||
|
if self.hashes[i]['sendCount'] > 0 and (
|
||||||
|
len(self.hashes[i]['peers']) == 0 or
|
||||||
|
self.hashes[i]['created'] + self.objectWait < time.time()):
|
||||||
|
del self.hashes[i]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
self.lastCleared = time.time()
|
||||||
|
|
||||||
|
def delete(self, objectHash=None):
|
||||||
if not hasattr(current_thread(), 'peer'):
|
if not hasattr(current_thread(), 'peer'):
|
||||||
return
|
return
|
||||||
|
if objectHash is None:
|
||||||
|
return
|
||||||
with self.lock:
|
with self.lock:
|
||||||
if objectHash in self.hashes and current_thread().peer in self.hashes[objectHash]:
|
try:
|
||||||
self.hashes[objectHash].remove(current_thread().peer)
|
if objectHash in self.hashes and current_thread().peer in self.hashes[objectHash]['peers']:
|
||||||
if len(self.hashes[objectHash]) == 0:
|
self.hashes[objectHash]['sendCount'] += 1
|
||||||
del self.hashes[objectHash]
|
self.hashes[objectHash]['peers'].remove(current_thread().peer)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
self.clearHashes(objectHash)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.hashes = {}
|
self.hashes = {}
|
||||||
|
|
||||||
def threadEnd(self):
|
def threadEnd(self):
|
||||||
while True:
|
with self.lock:
|
||||||
try:
|
for objectHash in self.hashes:
|
||||||
with self.lock:
|
try:
|
||||||
for objectHash in self.hashes:
|
if current_thread().peer in self.hashes[objectHash]['peers']:
|
||||||
if current_thread().peer in self.hashes[objectHash]:
|
self.hashes[objectHash]['peers'].remove(current_thread().peer)
|
||||||
self.hashes[objectHash].remove(current_thread().peer)
|
except KeyError:
|
||||||
if len(self.hashes[objectHash]) == 0:
|
pass
|
||||||
del self.hashes[objectHash]
|
self.clearHashes()
|
||||||
except (KeyError, RuntimeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user