mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-24 22:32:16 +00:00
Handle timeout errors better in IPFS, and looping http fetch to handle ERR_INSUFFICIENT_RESOURCES
This commit is contained in:
parent
0eca80e2dc
commit
daaade2ae2
@ -236,7 +236,7 @@ class TransportIPFS extends Transport {
|
|||||||
const ipfspath = TransportIPFS.ipfsFrom(url) // Need because dag.get has different requirement than file.cat
|
const ipfspath = TransportIPFS.ipfsFrom(url) // Need because dag.get has different requirement than file.cat
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await utils.p_timeout(this.ipfs.dag.get(cid), timeoutMS); // Will reject and throw TimeoutError if times out
|
const res = await utils.p_timeout(this.ipfs.dag.get(cid), timeoutMS, "Timed out IPFS fetch of "+TransportIPFS._stringFrom(cid)); // Will reject and throw TimeoutError if times out
|
||||||
// noinspection Annotator
|
// noinspection Annotator
|
||||||
if (res.remainderPath.length)
|
if (res.remainderPath.length)
|
||||||
{ // noinspection ExceptionCaughtLocallyJS
|
{ // noinspection ExceptionCaughtLocallyJS
|
||||||
@ -268,9 +268,10 @@ class TransportIPFS extends Transport {
|
|||||||
} catch (err) { // TimeoutError or could be some other error from IPFS etc
|
} catch (err) { // TimeoutError or could be some other error from IPFS etc
|
||||||
console.log("Caught misc error in TransportIPFS.p_rawfetch trying IPFS", err.message);
|
console.log("Caught misc error in TransportIPFS.p_rawfetch trying IPFS", err.message);
|
||||||
try {
|
try {
|
||||||
|
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url)
|
||||||
return await utils.p_timeout(
|
return await utils.p_timeout(
|
||||||
httptools.p_GET(TransportIPFS.ipfsGatewayFrom(url)), // Returns a buffer
|
httptools.p_GET(ipfsurl), // Returns a buffer
|
||||||
timeoutMS)
|
timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Caught misc error in TransportIPFS.p_rawfetch trying gateway", err.message);
|
console.log("Caught misc error in TransportIPFS.p_rawfetch trying gateway", err.message);
|
||||||
throw err;
|
throw err;
|
||||||
|
4
dist/dweb-transports-bundle.js
vendored
4
dist/dweb-transports-bundle.js
vendored
File diff suppressed because one or more lines are too long
20
httptools.js
20
httptools.js
@ -21,6 +21,22 @@ if (typeof(fetch) === "undefined") {
|
|||||||
|
|
||||||
httptools = {};
|
httptools = {};
|
||||||
|
|
||||||
|
async function loopfetch(req, ms, count, what) {
|
||||||
|
let lasterr;
|
||||||
|
while (count--) {
|
||||||
|
try {
|
||||||
|
return await fetch(req);
|
||||||
|
} catch(err) {
|
||||||
|
lasterr = err;
|
||||||
|
console.log("Delaying", what,"by", ms, "because", err.message);
|
||||||
|
await new Promise(resolve => {setTimeout(() => { resolve(); },ms)})
|
||||||
|
ms = ms*(1+Math.random()); // Spread out delays incase all requesting same time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("Looping",what,"failed");
|
||||||
|
throw(lasterr);
|
||||||
|
}
|
||||||
|
|
||||||
httptools.p_httpfetch = async function(httpurl, init, verbose) { // Embrace and extend "fetch" to check result etc.
|
httptools.p_httpfetch = async function(httpurl, init, verbose) { // Embrace and extend "fetch" to check result etc.
|
||||||
/*
|
/*
|
||||||
Fetch a url
|
Fetch a url
|
||||||
@ -33,7 +49,9 @@ httptools.p_httpfetch = async function(httpurl, init, verbose) { // Embrace and
|
|||||||
if (verbose) console.log("httpurl=%s init=%o", httpurl, init);
|
if (verbose) console.log("httpurl=%s init=%o", httpurl, init);
|
||||||
//console.log('CTX=',init["headers"].get('Content-Type'))
|
//console.log('CTX=',init["headers"].get('Content-Type'))
|
||||||
// Using window.fetch, because it doesn't appear to be in scope otherwise in the browser.
|
// Using window.fetch, because it doesn't appear to be in scope otherwise in the browser.
|
||||||
let response = await fetch(new Request(httpurl, init));
|
let req = new Request(httpurl, init);
|
||||||
|
//let response = await fetch(new Request(httpurl, init)).catch(err => console.exception(err));
|
||||||
|
let response = await loopfetch(req, 500, 10, "fetching "+httpurl);
|
||||||
// fetch throws (on Chrome, untested on Firefox or Node) TypeError: Failed to fetch)
|
// fetch throws (on Chrome, untested on Firefox or Node) TypeError: Failed to fetch)
|
||||||
// Note response.body gets a stream and response.blob gets a blob and response.arrayBuffer gets a buffer.
|
// Note response.body gets a stream and response.blob gets a blob and response.arrayBuffer gets a buffer.
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user