blob_availability fix

This commit is contained in:
Lex Berezhny 2019-01-10 18:14:57 -05:00
parent 69ccbda168
commit ebdb33bd11
2 changed files with 16 additions and 20 deletions

View file

@ -938,7 +938,7 @@ class Daemon(metaclass=JSONRPCServerType):
reachable_peers = []
unreachable_peers = []
try:
peers = await d2f(self.jsonrpc_peer_list(blob_hash, search_timeout))
peers = await self.jsonrpc_peer_list(blob_hash, search_timeout)
peer_infos = [{"peer": Peer(x['host'], x['port']),
"blob_hash": blob_hash,
"timeout": blob_timeout} for x in peers]
@ -946,16 +946,14 @@ class Daemon(metaclass=JSONRPCServerType):
dl_peers = []
dl_results = []
for peer_info in peer_infos:
d = downloader.download_temp_blob_from_peer(**peer_info)
dl.append(d)
dl.append(downloader.download_temp_blob_from_peer(**peer_info))
dl_peers.append("%s:%i" % (peer_info['peer'].host, peer_info['peer'].port))
for dl_peer, (success, download_result) in zip(dl_peers, (await d2f(defer.DeferredList(dl)))):
if success:
if download_result:
reachable_peers.append(dl_peer)
else:
unreachable_peers.append(dl_peer)
dl_results.append(download_result)
for dl_peer, download_result in zip(dl_peers, await asyncio.gather(*dl)):
if download_result:
reachable_peers.append(dl_peer)
else:
unreachable_peers.append(dl_peer)
dl_results.append(download_result)
is_available = any(dl_results)
except Exception as err:
return {'error': "Failed to get peers for blob: %s" % err}

View file

@ -4,6 +4,7 @@ import tempfile
from twisted.internet import defer, threads, reactor
from lbrynet.extras.compat import d2f
from lbrynet.blob.blob_file import BlobFile
from lbrynet.p2p.BlobManager import DiskBlobManager
from lbrynet.p2p.RateLimiter import DummyRateLimiter
@ -94,16 +95,13 @@ class SinglePeerDownloader:
yield connection_manager.stop()
defer.returnValue(result)
@defer.inlineCallbacks
def download_temp_blob_from_peer(self, peer, timeout, blob_hash):
tmp_dir = yield threads.deferToThread(tempfile.mkdtemp)
tmp_storage = SQLiteStorage(tmp_dir)
yield tmp_storage.setup()
async def download_temp_blob_from_peer(self, peer, timeout, blob_hash):
tmp_storage = SQLiteStorage(':memory:')
await tmp_storage.open()
tmp_dir = tempfile.mkdtemp()
tmp_blob_manager = DiskBlobManager(tmp_dir, tmp_storage)
try:
result = yield self.download_blob_from_peer(peer, timeout, blob_hash, tmp_blob_manager)
return await d2f(self.download_blob_from_peer(peer, timeout, blob_hash, tmp_blob_manager))
finally:
yield tmp_blob_manager.stop()
yield tmp_storage.stop()
yield threads.deferToThread(shutil.rmtree, tmp_dir)
defer.returnValue(result)
await tmp_storage.close()
shutil.rmtree(tmp_dir)