From 62a38621db2d088dcc7412e812063f3464d45cb8 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 28 Mar 2016 20:55:33 -0700 Subject: [PATCH] Don't create new outgoing TCP connections when torrent is done --- lib/swarm.js | 2 ++ lib/torrent.js | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/swarm.js b/lib/swarm.js index 6f169cb3..7edee82b 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -364,6 +364,8 @@ Swarm.prototype._drain = function () { conn.on('close', function () { if (self.destroyed) return + // TODO: If torrent is done, do not try to reconnect after a timeout + if (peer.retries >= RECONNECT_WAIT.length) { debug( 'conn %s closed: will not re-add (max %s attempts)', diff --git a/lib/torrent.js b/lib/torrent.js index eaa5396d..aad46b58 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -299,7 +299,11 @@ Torrent.prototype._onSwarmListening = function () { port: self.client.torrentPort }) self.discovery.on('error', self._onError.bind(self)) - self.discovery.on('peer', self.addPeer.bind(self)) + self.discovery.on('peer', function (peer) { + // Don't create new outgoing TCP connections when torrent is done + if (typeof peer === 'string' && self.done) return + self.addPeer(peer) + }) // expose discovery events reemit(self.discovery, self, ['trackerAnnounce', 'dhtAnnounce', 'warning'])