Azure 函数 - 节点应用程序中的端口耗尽

Azure Function - port exhaustion in node app

我正在尝试构建一个发出大量传出 HTTP 请求的 Azure 函数。它似乎会周期性地达到极限,并且所有请求都会超时几分钟。然后它再次开始工作并通过请求。

通过四处搜索,这听起来可能是由于机器上的端口耗尽 运行使用该函数,但我不明白如何调试它或在 Node 应用程序中使用request 图书馆。听起来 Node 应该将连接池化以防止这种情况发生。我完全不确定端口耗尽是问题所在,因为我不能在函数上使用 netstat。而且我的笔记本电脑 运行ning 从来没有问题。

这是一个简单的 Azure Functions,它一次触发大量请求,并说明了这个问题:

const cp = require('child_process');
const request = require('request');

module.exports = function (context, myTimer) {
    context.log('Starting');

    function doCall(cb) {
        const url = 'https://jsonplaceholder.typicode.com/posts';
        request(url, (err) => {
            if (err) {
                context.log("error: " + err.toString());
            }

            cb();
        });
    }

    let i = 500;
    doCall(function iterate() {
        if (i-- > 0) {
            context.log('iterate ' + i);
            doCall(iterate);
        } else {
            context.log('done');
            context.done();
        }
    });
};

我看到了运行成功,超时几分钟,运行再次成功...

@david-ebbo,你的建议很有帮助。根据您发布的 link,我可以使用 http 代理来限制套接字池。 运行 需要一段时间,但现在没有请求超时。

我上面的例子现在看起来像这样:

const cp = require('child_process');
const request = require('request');
const http = require('http');

module.exports = function (context, myTimer) {
    context.log('Starting with agent');
    const pool = new http.Agent();
    pool.maxSockets = 4;

    function doCall(cb) {
        const url = 'https://jsonplaceholder.typicode.com/posts';
        request({ url, pool }, (err) => {
            if (err) {
                context.log("error: " + err.toString());
            }

            cb();
        });
    }

    let i = 500;
    doCall(function iterate() {
        if (i-- > 0) {
            context.log('iterate ' + i);
            doCall(iterate);
        } else {
            context.log('done');
            context.done();
        }
    });
};