Commit 254dd5cd authored by Julien Muchembled's avatar Julien Muchembled

Fix creation of tunnel ignoring routing table updates until all peers are tried

parent daed799b
...@@ -328,6 +328,9 @@ class TunnelManager(object): ...@@ -328,6 +328,9 @@ class TunnelManager(object):
logging.trace('Connection with %u/%u killed', logging.trace('Connection with %u/%u killed',
int(prefix, 2), len(prefix)) int(prefix, 2), len(prefix))
def _newTunnelScore(self, prefix):
return (prefix in self._neighbour_set) + random.random()
def _makeTunnel(self, prefix, address): def _makeTunnel(self, prefix, address):
if prefix in self._served or prefix in self._connection_dict: if prefix in self._served or prefix in self._connection_dict:
return False return False
...@@ -358,9 +361,7 @@ class TunnelManager(object): ...@@ -358,9 +361,7 @@ class TunnelManager(object):
# before calling _makeNewTunnels again. # before calling _makeNewTunnels again.
self._connecting.clear() self._connecting.clear()
distant_peers = self._distant_peers distant_peers = self._distant_peers
if len(distant_peers) < count or 0 < self._disconnected < time.time(): if route_dumped:
if not route_dumped:
return True
logging.debug('Analyze routes ...') logging.debug('Analyze routes ...')
neighbours = self.ctl.neighbours neighbours = self.ctl.neighbours
# Collect all nodes known by Babel # Collect all nodes known by Babel
...@@ -370,6 +371,7 @@ class TunnelManager(object): ...@@ -370,6 +371,7 @@ class TunnelManager(object):
if prefix) if prefix)
# Keep only distant peers. # Keep only distant peers.
distant_peers[:] = peers.difference(neighbours) distant_peers[:] = peers.difference(neighbours)
distant_peers.sort(key=self._newTunnelScore)
# Check whether we're connected to the network. # Check whether we're connected to the network.
registry = self.peer_db.registry_prefix registry = self.peer_db.registry_prefix
if (registry == self._prefix or registry in peers if (registry == self._prefix or registry in peers
...@@ -397,19 +399,13 @@ class TunnelManager(object): ...@@ -397,19 +399,13 @@ class TunnelManager(object):
count -= self._makeTunnel(*peer) count -= self._makeTunnel(*peer)
if not count: if not count:
return return
elif len(distant_peers) < count or 0 < self._disconnected < time.time():
return True
if distant_peers: if distant_peers:
# Normal operation. Choose peers to connect to by looking at the # Normal operation. Choose peers to connect to by looking at the
# routing table. # routing table.
neighbour_set = self._neighbour_set.intersection(distant_peers)
while count and distant_peers: while count and distant_peers:
if neighbour_set: peer = distant_peers.pop()
peer = neighbour_set.pop()
i = distant_peers.index(peer)
else:
i = random.randrange(len(distant_peers))
peer = distant_peers[i]
distant_peers[i] = distant_peers[-1]
del distant_peers[-1]
address = self.peer_db.getAddress(peer) address = self.peer_db.getAddress(peer)
if address: if address:
count -= self._makeTunnel(peer, address) count -= self._makeTunnel(peer, address)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment