如何调试冻结的 axios http 请求?

发布于 2025-01-20 19:25:35 字数 3535 浏览 1 评论 0原文

我正在通过 node.js 发出 http 请求,并且 axios 在数百次迭代后冻结。

我已经使用了一些 console.logs 来缩小问题范围,但我不知道如何找出根本问题并修复它......任何有关如何调试此问题的提示将非常感激。

上下文:在此工作线程中,调用 getTokenData 函数,这就是 axios http 请求所在的位置。

//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
                    });
                })
        }

    })
}


在绝望中,我什至将调用包装在 setTimeout 命令中,因为我认为 axios“冻结”的原因可能是每隔一定时间间隔向 ipfs 网关发出太多请求或类似的情况。情况似乎并非如此,因为有时 axios 在几百个请求后冻结,有时在大约 1k-1,5k 个请求后冻结。

这就是 console.logs 停止而没有任何错误消息的方式。

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.

我想知道如何进一步分析并找到实际问题...还想知道这是否是 axios/node.js 的事情,也许另一种请求方法可以做得更好?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文