Skip to content

Commit

Permalink
Refactored DHT check in LibtorrentMgr
Browse files Browse the repository at this point in the history
Now, only the starting of the DHT is executed on the thread pool. The check itself is executed on the reactor thread.
  • Loading branch information
devos50 committed Jul 12, 2016
1 parent 7ae5e6f commit 3bcea68
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions Tribler/Core/Libtorrent/LibtorrentMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from copy import deepcopy
from shutil import rmtree

from twisted.internet import reactor
from twisted.internet import reactor, threads
import libtorrent as lt
from Tribler.Core.Utilities.torrent_utils import get_info_from_handle
from Tribler.Core.TorrentDef import TorrentDef, TorrentDefNoMetainfo
Expand Down Expand Up @@ -473,31 +473,24 @@ def _task_check_reachability(self):

@call_on_reactor_thread
def _schedule_next_check(self, delay, retries_left):
self.register_task(u'check_dht', reactor.callLater(delay, self._task_check_dht, retries_left))
self.register_task(u'check_dht', reactor.callLater(delay, self.do_dht_check, retries_left))

def _task_check_dht(self, retries_left):
def do_dht_check(self, retries_left):
# Sometimes the dht fails to start. To workaround this issue we monitor the #dht_nodes, and restart if needed.

def do_dht_check():
lt_session = self.get_session()
if lt_session:
dht_nodes = lt_session.status().dht_nodes
if dht_nodes <= 25:
if dht_nodes >= 5 and retries_left > 0:
self._logger.info(u"No enough DHT nodes %s, will try again", lt_session.status().dht_nodes)
self._schedule_next_check(5, retries_left - 1)
else:
self._logger.info(u"No enough DHT nodes %s, will restart DHT", lt_session.status().dht_nodes)
lt_session.start_dht()
self._schedule_next_check(10, 1)
else:
self._logger.info("dht is working enough nodes are found (%d)", self.get_session().status().dht_nodes)
self.dht_ready = True
return
lt_session = self.get_session()
dht_nodes = lt_session.status().dht_nodes
if dht_nodes <= 25:
if dht_nodes >= 5 and retries_left > 0:
self._logger.info(u"No enough DHT nodes %s, will try again", lt_session.status().dht_nodes)
self._schedule_next_check(5, retries_left - 1)
else:
self._logger.info(u"No enough DHT nodes %s, will restart DHT", lt_session.status().dht_nodes)
threads.deferToThread(lt_session.start_dht)
self._schedule_next_check(10, 1)

self.trsession.lm.threadpool.call(0, do_dht_check)
else:
self._logger.info("dht is working enough nodes are found (%d)", self.get_session().status().dht_nodes)
self.dht_ready = True

def _map_call_on_ltsessions(self, hops, funcname, *args, **kwargs):
if hops is None:
Expand Down

0 comments on commit 3bcea68

Please sign in to comment.