diff options
author | John Hiesey <john@hiesey.com> | 2021-04-13 06:25:27 +0300 |
---|---|---|
committer | John Hiesey <john@hiesey.com> | 2021-04-14 04:33:07 +0300 |
commit | dcde609fffd1b7e66f7ea4881e8838d9f5915ec2 (patch) | |
tree | 06f823c64fc00d2039b16cec2d5f6f2e2cdab7ea /lib | |
parent | dddd652ac29eb6547761bb84a8d6c624bc1152bd (diff) |
Add timeouts and retry inside webconn
Diffstat (limited to 'lib')
-rw-r--r-- | lib/webconn.js | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/webconn.js b/lib/webconn.js index ca6e0a7..c957fbb 100644 --- a/lib/webconn.js +++ b/lib/webconn.js @@ -7,6 +7,9 @@ const Wire = require('bittorrent-protocol') const VERSION = require('../package.json').version +const SOCKET_TIMEOUT = 60000 +const RETRY_DELAY = 10000 + /** * Converts requests for torrent blocks into http range requests. * @param {string} url web seed url @@ -54,15 +57,17 @@ class WebConn extends Wire { this.on('request', (pieceIndex, offset, length, callback) => { debug('request pieceIndex=%d offset=%d length=%d', pieceIndex, offset, length) this.httpRequest(pieceIndex, offset, length, (err, data) => { - // TODO: timeout - if (err) { // Cancel all in progress requests for this piece this.lt_donthave.donthave(pieceIndex) - this.bitfield.set(pieceIndex, false) - // TODO: delay before re-advertising - this.have(pieceIndex) + // Wait a little while before saying the webseed has the failed piece again + const retryTimeout = setTimeout(() => { + if (this.destroyed) return + + this.have(pieceIndex) + }, RETRY_DELAY) + if (retryTimeout.unref) retryTimeout.unref() } callback(err, data) @@ -133,7 +138,8 @@ class WebConn extends Wire { headers: { 'user-agent': `WebTorrent/${VERSION} (https://webtorrent.io)`, range: `bytes=${start}-${end}` - } + }, + timeout: SOCKET_TIMEOUT } function onResponse (res, data) { if (res.statusCode < 200 || res.statusCode >= 300) { |