From 9c86b65b4c44cca46235e6290b63fb7c0c043ffb Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 4 Dec 2022 16:48:20 -0600 Subject: [PATCH 1/3] fix: replace offer when peer is disconnected --- src/torrent.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/torrent.js b/src/torrent.js index 2855c30..6a6af52 100644 --- a/src/torrent.js +++ b/src/torrent.js @@ -55,9 +55,9 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { .slice(0, hashLimit) ) - const makeOffers = () => + const makeOffers = (howMany) => fromEntries( - new Array(offerPoolSize).fill().map(() => { + new Array(howMany).fill().map(() => { const peer = initPeer(true, false, config.rtcConfig) return [ @@ -67,6 +67,9 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { }) ) + + const makeOfferPool = () => makeOffers(offerPoolSize) + const onSocketMessage = async (socket, e) => { const infoHash = await infoHashP let val @@ -123,7 +126,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { ) ) peer.on(events.connect, () => onConnect(peer, val.peer_id)) - peer.on(events.close, () => onDisconnect(val.peer_id)) + peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id)) peer.signal( key ? {...val.offer, sdp: await decrypt(key, val.offer.sdp)} : val.offer ) @@ -149,7 +152,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { peer.on(events.connect, () => onConnect(peer, val.peer_id, val.offer_id) ) - peer.on(events.close, () => onDisconnect(val.peer_id)) + peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id)) peer.signal( key ? {...val.answer, sdp: await decrypt(key, val.answer.sdp)} @@ -207,7 +210,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { cleanPool() } - offerPool = makeOffers() + offerPool = makeOfferPool() trackerUrls.forEach(async url => { const socket = await makeSocket(url, infoHash) @@ -239,7 +242,18 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { } } - const onDisconnect = id => delete connectedPeers[id] + const onDisconnect = (peerId, offerId) => { + delete connectedPeers[peerId] + const { peer } = offerPool[offerId] + + if (peer) { + peer.destroy() + delete offerPool[offerId] + offerPool = {...offerPool, ...makeOffers(1)} + } else { + console.error(`peer ${peerId} for offer ${offerId} not found`) + } + } let announceSecs = defaultAnnounceSecs let announceInterval = setInterval(announceAll, announceSecs * 1000) From 50864ccace7192dd80e5a4daa432b111a4ace1fc Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 4 Dec 2022 17:29:50 -0600 Subject: [PATCH 2/3] fix: maintain correct reference to peer to destroy upon disconnection --- src/torrent.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/torrent.js b/src/torrent.js index 6a6af52..1257c00 100644 --- a/src/torrent.js +++ b/src/torrent.js @@ -126,7 +126,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { ) ) peer.on(events.connect, () => onConnect(peer, val.peer_id)) - peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id)) + peer.on(events.close, () => onDisconnect(peer, val.peer_id, val.offer_id)) peer.signal( key ? {...val.offer, sdp: await decrypt(key, val.offer.sdp)} : val.offer ) @@ -152,7 +152,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { peer.on(events.connect, () => onConnect(peer, val.peer_id, val.offer_id) ) - peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id)) + peer.on(events.close, () => onDisconnect(peer, val.peer_id, val.offer_id)) peer.signal( key ? {...val.answer, sdp: await decrypt(key, val.answer.sdp)} @@ -242,17 +242,11 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { } } - const onDisconnect = (peerId, offerId) => { + const onDisconnect = (peer, peerId, offerId) => { delete connectedPeers[peerId] - const { peer } = offerPool[offerId] - - if (peer) { - peer.destroy() - delete offerPool[offerId] - offerPool = {...offerPool, ...makeOffers(1)} - } else { - console.error(`peer ${peerId} for offer ${offerId} not found`) - } + delete offerPool[offerId] + peer.destroy() + offerPool = {...offerPool, ...makeOffers(1)} } let announceSecs = defaultAnnounceSecs From 65ca75baac7a9df40fd217c6533dea2b059a15e7 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 4 Dec 2022 17:55:10 -0600 Subject: [PATCH 3/3] fix: only replenish offerPool if needed --- src/torrent.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/torrent.js b/src/torrent.js index 1257c00..6d55913 100644 --- a/src/torrent.js +++ b/src/torrent.js @@ -244,9 +244,14 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => { const onDisconnect = (peer, peerId, offerId) => { delete connectedPeers[peerId] - delete offerPool[offerId] peer.destroy() - offerPool = {...offerPool, ...makeOffers(1)} + + const isInOfferPool = offerId in offerPool + + if (isInOfferPool) { + delete offerPool[offerId] + offerPool = {...offerPool, ...makeOffers(1)} + } } let announceSecs = defaultAnnounceSecs