refactor closing peers

This commit is contained in:
Job Evers-Meltzer 2017-01-11 11:40:53 -06:00
parent 2449604844
commit 15c5075d38

View file

@ -46,30 +46,29 @@ class ConnectionManager(object):
if self._next_manage_call is not None and self._next_manage_call.active() is True: if self._next_manage_call is not None and self._next_manage_call.active() is True:
self._next_manage_call.cancel() self._next_manage_call.cancel()
self._next_manage_call = None self._next_manage_call = None
closing_deferreds = []
for peer in self._peer_connections.keys():
def close_connection(p): return self._close_peers()
log.info(
"Abruptly closing a connection to %s due to downloading being paused", p)
if self._peer_connections[p].factory.p is not None: def _close_peers(self):
d = self._peer_connections[p].factory.p.cancel_requests()
else:
d = defer.succeed(True)
def disconnect_peer(): def disconnect_peer(p):
d = defer.Deferred() d = defer.Deferred()
self._connections_closing[p] = d self._connections_closing[p] = d
self._peer_connections[p].connection.disconnect() self._peer_connections[p].connection.disconnect()
if p in self._peer_connections: if p in self._peer_connections:
del self._peer_connections[p] del self._peer_connections[p]
return d return d
d.addBoth(lambda _: disconnect_peer()) def close_connection(p):
return d log.info("Abruptly closing a connection to %s due to downloading being paused", p)
if self._peer_connections[p].factory.p is not None:
d = self._peer_connections[p].factory.p.cancel_requests()
else:
d = defer.succeed(True)
d.addBoth(lambda _: disconnect_peer(p))
return d
closing_deferreds.append(close_connection(peer)) closing_deferreds = [close_connection(peer) for peer in self._peer_connections.keys()]
return defer.DeferredList(closing_deferreds) return defer.DeferredList(closing_deferreds)
@defer.inlineCallbacks @defer.inlineCallbacks