diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-10-11 16:52:48 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-10-11 16:52:48 +0200 |
| commit | aca58f66df5c21be71a7159620d0b50bb969199f (patch) | |
| tree | 28bcb0e834d9c05a25b37e56ad71c424924d93fc /packages/meshbay-hub/src/meshbay_hub/static/file-utils.js | |
| parent | 9d38418817d1894b31c93ece0113e503d82167cd (diff) | |
| download | meshbay-aca58f66df5c21be71a7159620d0b50bb969199f.tar.gz | |
feat: spread a large download over eight connections
One WebRTC connection carries 5 to 6 MB/s whatever the line can do.
A leased download of 64 MB or more now opens seven more to its node,
and a lease serves the other connections of its device in its group.
Measured through the whole stack: 5 MB/s before, 24 after.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/static/file-utils.js')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/static/file-utils.js | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/file-utils.js b/packages/meshbay-hub/src/meshbay_hub/static/file-utils.js index 294455a..585558f 100644 --- a/packages/meshbay-hub/src/meshbay_hub/static/file-utils.js +++ b/packages/meshbay-hub/src/meshbay_hub/static/file-utils.js @@ -48,6 +48,11 @@ const CHUNK_SIZE = 1024 * 1024; // fragmented MP4. A stream that arrives in gulps has no margin for a network // that hesitates, and looks like a hang while it is quiet. const PIPELINE_WINDOW = 8; +// A leased download with at least this much left opens the transport's extra +// connections (`lanes` in transport.js). Below it the handshakes, half a second +// to a second each, cost more than they save; lanes already open are used +// whatever the size. +const LANE_MIN_CHUNKS = 64; // The most this code will ever collect in the page. // @@ -392,10 +397,24 @@ async function pipelinedDownload(transport, gekKey, fileId, totalChunks, onChunk let nextSend = fromChunk, nextRecv = fromChunk; const inflight = new Array(totalChunks); + // Chunk i goes over lane i modulo the lanes open at that moment, two chunks + // in flight per lane so none waits on the slowest. Order is restored below, + // where each chunk is awaited by its index. A lane that fails a request is + // dropped and the chunk asked for again on the main connection, which has + // the retries and the reconnect. const fire = () => { - while (nextSend < totalChunks && nextSend - nextRecv < PIPELINE_WINDOW) { - inflight[nextSend] = _fetchChunkResilient(transport, fileId, nextSend, tr, - signal); + const lanes = (tr && transport.lanes) + ? transport.lanes(totalChunks - nextRecv >= LANE_MIN_CHUNKS) : [transport]; + const depth = Math.max(PIPELINE_WINDOW, 2 * lanes.length); + while (nextSend < totalChunks && nextSend - nextRecv < depth) { + const index = nextSend; + const lane = lanes[index % lanes.length]; + inflight[index] = lane === transport + ? _fetchChunkResilient(transport, fileId, index, tr, signal) + : lane.fetchChunk(fileId, index, tr).catch(() => { + transport.dropLane(lane); + return _fetchChunkResilient(transport, fileId, index, tr, signal); + }); nextSend++; } }; |