Skip to content

Commit

Permalink
Upstream controller don't abort all requests (#38)
Browse files Browse the repository at this point in the history
* Upstream controller don't abort all requests

* If upstream controller aborted, stop
  • Loading branch information
Diego Rodríguez Baquero authored Nov 2, 2023
1 parent 7e9dd0e commit 4bec3a3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,13 @@ export class Saturn {
* @param {string} [opts.url]
* @param {number} [opts.connectTimeout=5000]
* @param {number} [opts.downloadTimeout=0]
* @param {AbortController} [opts.controller]
* @returns {Promise<AsyncIterable<Uint8Array>>}
*/
async * fetchContentWithFallback (cidPath, opts = {}) {
const upstreamController = opts.controller;
delete opts.controller;

let lastError = null
// we use this to checkpoint at which chunk a request failed.
// this is temporary until range requests are supported.
Expand All @@ -261,6 +265,13 @@ export class Saturn {
}

const fetchContent = async function * () {
const controller = new AbortController();
opts.controller = controller;
if (upstreamController) {
upstreamController.signal.addEventListener('abort', () => {
controller.abort();
});
}
let byteCount = 0
const byteChunks = await this.fetchContent(cidPath, opts)
for await (const chunk of byteChunks) {
Expand Down Expand Up @@ -298,7 +309,7 @@ export class Saturn {
let fallbackCount = 0
const nodes = this.nodes
for (let i = 0; i < nodes.length; i++) {
if (fallbackCount > this.opts.fallbackLimit) {
if (fallbackCount > this.opts.fallbackLimit || upstreamController?.signal.aborted) {
return
}
if (opts.raceNodes) {
Expand Down

0 comments on commit 4bec3a3

Please sign in to comment.