Handle timeout errors better in IPFS, and looping http fetch to handle ERR_INSUFFICIENT_RESOURCES

This commit is contained in:
Mitra Ardron 2018-06-08 14:39:33 -07:00
parent 0eca80e2dc
commit daaade2ae2
3 changed files with 25 additions and 6 deletions

View File

@ -236,7 +236,7 @@ class TransportIPFS extends Transport {
const ipfspath = TransportIPFS.ipfsFrom(url) // Need because dag.get has different requirement than file.cat
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
if (res.remainderPath.length)
{ // noinspection ExceptionCaughtLocallyJS
@ -268,9 +268,10 @@ class TransportIPFS extends Transport {
} 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);
try {
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url)
return await utils.p_timeout(
httptools.p_GET(TransportIPFS.ipfsGatewayFrom(url)), // Returns a buffer
timeoutMS)
httptools.p_GET(ipfsurl), // Returns a buffer
timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
} catch (err) {
console.log("Caught misc error in TransportIPFS.p_rawfetch trying gateway", err.message);
throw err;

File diff suppressed because one or more lines are too long

View File

@ -21,6 +21,22 @@ if (typeof(fetch) === "undefined") {
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.
/*
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);
//console.log('CTX=',init["headers"].get('Content-Type'))
// 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)
// Note response.body gets a stream and response.blob gets a blob and response.arrayBuffer gets a buffer.
if (response.ok) {