0

I am making http requests via node.js and axios freezes after some hundred iterations.

I already used some console.logs to narrow down the problem but I have no idea how to find out what's the underlying problem and fix it... any hints on how to debug this would be very much appreciated.

Context: in this worker thread a getTokenData function is called, that's where the axios http request is.

//worker.js

parentPort.on("message", async (data: dataFromParentPort) => {
// ... variable definitions

for (let token_index = 0;
            token_index < 3000;
            token_index++) {

    console.log(`Looping through HTTP requests... request ${token_index}/${supply}`);
    let url: string = baseURI + token_index.toString();
  
await getTokenData(url, token_index)
                .then((json: any) => {
                    console.log("Promise resolved, got status " + json.status)
                    // do stuff with json
                })
                .catch((rej) => {
                    console.log("[Error] Catched this: " + JSON.stringify(rej))
                })
console.log(`${guildId}/${address}: Token #${token_index} cached.`)
}

function getTokenData(_url: string, _tokenId: number) {
    return new Promise( (resolve, reject) => {
        console.log("Init Http Request...")
        if ((_tokenId % 50 == 0)) {
            console.log("Pausing...");
            setTimeout(() => {
                console.log("Axios is starting request.")
                axios.get(_url)
                    .then(response => {
                        console.log("Axios got a response: " + response.data.name)
                        resolve({
                            status: 1,
                            data: response.data
                        });
                    })
                    .catch((error: any) => {
                        console.log();
                        console.error("Error: " + error.message);
                        reject({
                            status: 0,
                            data: error
                        });
                    })
            }, 3000)
        }

        else {

          
            console.log("Axios is starting request.")
            axios.get(_url)
                .then(response => {
                    console.log("Axios got a response: " + response.data.name)
                    resolve({
                        status: 1,
                        data: response.data
                    });
                })
                .catch((error: any) => {
                    console.log();
                    console.error("Error: " + error.message);
                    reject({
                        status: 0,
                        data: error
                    });
                })
        }

    })
}


In my desperation I even wrapped the call in a setTimeout command because I thought the reason for axios "freezing" might be too many requests to the ipfs gateway per certain intervals or something like that. It seems not to be the case, because sometimes axios freezes after a few hundred, sometimes after roughly 1k-1,5k requests.

This is how the console.logs just stop without any error messages.

Looping through HTTP requests... request 1469/3333
Init Http Request...
Axios is starting request.
Axios got a response: Angry Ape #1469
Promise resolved, got status 1
123/0x77640cf3f89a4f1b5ca3a1e5c87f3f5b12ebf87e: Token #1469 cached. Looping.
Looping through HTTP requests... request 1470/3333
Init Http Request...
Axios is starting request.
Axios got a response: Angry Ape #1470
Promise resolved, got status 1
123/0x77640cf3f89a4f1b5ca3a1e5c87f3f5b12ebf87e: Token #1470 cached. Looping.
Looping through HTTP requests... request 1471/3333
Init Http Request...
Axios is starting request.

I wonder how I could analyze this further and find the actual problem... also wonder wether this is an axios/node.js thing and maybe another request method could do better?

Juno
  • 211
  • 5
  • 17
  • 2
    Not the cause of your issue, but avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 11 '22 at 00:48
  • Is there any other code running? Does that also "freeze"? A better description might be that `axios` "no longer completes". – Bergi Apr 11 '22 at 00:49
  • @Bergi no other code running at the same time here. you're right with the description, will try to edit the question – Juno Apr 11 '22 at 00:59
  • Does it always stop sending requests at 1471? – Joshua Wood Apr 11 '22 at 01:42
  • No, @JoshuaWood, it stops at different iterations points – Juno Apr 11 '22 at 12:14
  • Is this for a discord bot or something? I'm not really sure exactly what could be going on, but `3000` requests seems like a lot. – Joshua Wood Apr 12 '22 at 01:34
  • it's just exercise and trying different methods for caching blockchain data. in this case caching NFT metadata – Juno Apr 13 '22 at 07:39
  • I am not sure if it was the promise antipattern, @Bergi, but after cleaning up the code and getting rid of those unnecessary Promises it works much better :) – Juno Apr 13 '22 at 07:43

0 Answers0