从我的Azure函数(在Node.js中运行,由EventHub消息触发)我想向一些外部页面发布一个帖子请求.
就像是:
module.exports = function (context,eventHubMessages) {
var http = require("http");
context.log('JavaScript Function triggered by eventHub messages ');
http.request(post_options,function(res){
...
})
context.done();
上面的代码可能会有效,但我怀疑这不是反模式.
想象一下在短时间内触发了数千个函数的情况 – 对于每次执行,我们需要创建一个HTTP客户端并创建一个连接……
从简短的研究中我发现了一些C#Azure Functions的解决方案:https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/
它使用静态HttpClient类.
我有一个问题,Node.js Azure功能中是否有类似的方法?或者任何其他方法来避免这个问题,在Node.js Azure Function执行之间共享一个对象?
An Agent is responsible for managing connection persistence and reuse
for HTTP clients. It maintains a queue of pending requests for a given
host and port,reusing a single socket connection for each until the
queue is empty,at which time the socket is either destroyed or put
into a pool where it is kept to be used again for requests to the same
host and port. Whether it is destroyed or pooled depends on the
keepAlive option.
资料来源:https://nodejs.org/api/http.html#http_class_http_agent
http.globalAgent.maxSockets默认为无穷大,因此,除非您限制此值,否则您的函数将耗尽套接字,您将看到您的请求开始失败.
此外,如果您计划连接到同一主机,则应在globalAgent / Agent上启用keep-alive以启用池化连接.