简体   繁体   English

nodejs,控制并发连接

[英]nodejs, control over concurrent connections

I am using nodejs async module for concurrent connectios, Now my backend server only can handle 1000 connections at a time, I am using async.mapLimit to limit the connections, each and every job of async.mapLimit does multiple connections, when I am sending the same request which does async.mapLimit from multiple browser at the same time, then I am getting EMFILE error from Server side 我正在使用nodejs异步模块进行并发connectios,现在我的后端服务器一次只能处理1000个连接,我正在使用async.mapLimit来限制连接,当我发送时,async.mapLimit的每个工作都会进行多个连接从多个浏览器同时执行async.mapLimit的相同请求,然后我从服务器端收到EMFILE错误

([Error: connect EMFILE] code: 'EMFILE', errno: 'EMFILE', syscall: 'connect'), 

My code somewhat looks like this: 我的代码看起来像这样:

async.mapLimit(jobList, 200, jobCallback, function(error, data) {
});

function jobCallback(job, callback) {
    /* Make multiple connections to to backend server, this number is 
       dynamic, here also I use async.mapLimit */
}

Now I want to implement some wrapper function top of this mapLimit or anything, irrespective of number of parallel requests, I want to limit the concurrent connections, even irrespective of number of client calls also, it may be slower, but I do not bother. 现在,我想在此mapLimit或其他任何东西的顶部实现一些包装函数,无论并行请求的数量如何,我都想限制并发连接,甚至也不管客户端调用的数量如何,它可能都比较慢,但我不会打扰。

How I can achieve this? 我该如何实现?

I am using restler library. 我正在使用Restler库。 I have tried to set 我试图设定

proto.globalAgent.maxSockets = 1000

to do concurrent 1000 connections at a time, but it seems it is not working. 一次并发1000个连接,但似乎不起作用。 Please advise. 请指教。

-M- -M-

You will have to control for throttling yourself, as that async instruction won't know if you have calls from other users adding to the 1000 limit. 您将必须控制自己的速度,因为该异步指令不会知道您是否收到来自其他用户的通话,通话数量增加到1000个限制。

In REST services, a service would typically send an http 429 response when such a limit is triggered, allowing your app to identify a bottleneck scenario and trigger a throttling mechanism. 在REST服务中,触发此类限制时,服务通常会发送http 429响应,从而使您的应用能够识别瓶颈情况并触发调节机制。

A common way to do that is via exponential backoff https://developers.google.com/api-client-library/java/google-http-java-client/backoff 一种常用的方法是通过指数补偿https://developers.google.com/api-client-library/java/google-http-java-client/backoff

I use the following line of code to manage the limit globally: 我使用以下代码行来全局管理限制:

require('events').EventEmitter.prototype._maxListeners = 1000;

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM