Node.js 套接字解释 [英] Node js socket explanation

查看:25
本文介绍了Node.js 套接字解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序将对远程 api 服务器进行大约一百万次调用.我可以将连接数量限制为例如 10 个吗?是否将最大套接字设置为 10 行?

I am building an application that will be making about a million calls to a remote api server. Will I be able to limit amount of connections to for example 10? Do I set max sockets to 10 will do it?

我试图了解这些参数的作用:

I am trying to understand what do these parameters do:

keepAlive: false,
maxSockets: 999,
maxFreeSockets: 1

在节点http get函数中,代码如下:

In node http get function, in the following code:

var inputData = [];

for(i=1; i<=5000;i++){
    inputData.push('number' + i);
}

var options = {
    host: "localhost",
    port: 80,
    path: "/text.txt",
    keepAlive: false,
    maxSockets: 999,
    maxFreeSockets: 1
}


var limit = inputData.length;
var counter = 0;

function fetchData(number){

    return new Promise(function(resolve, reject){
        var http = require('http');

        fetch  = function(resp){
            var body = '';
            resp.on('data',function(chunk){
                body += chunk;
            })
            resp.on('end',function(){
                console.log(resp)
                resolve()
            })
            resp.on('error',function(err){
                console.log('error');
            })
        }
        var req = http.request(options, fetch);

        req.end();

    })
}



Promise.all(inputData.map(number => fetchData(number))).then(function(results) {
    console.log('finished');
    connection.end();

})
.catch(function(error) {
    console.log('there wa an error');
    console.log(error);
});

推荐答案

您真的不想触发 1,000,000 个请求,并希望 maxSockets 一次将其管理到 100 个.有很多原因表明这不是一种很好的做事方式.相反,您应该使用自己的代码将实时连接的数量管理到 100 个.

You really don't want to fire off 1,000,000 requests and somehow hope that maxSockets manages it to 100 at a time. There are a whole bunch of reasons why that is not a great way to do things. Instead, you should use your own code that manages the number of live connections to 100 at a time.

有很多方法可以做到这一点:

There are a number of ways to do that:

  1. 编写自己的代码,启动 100 次,然后每完成一个,就启动下一个.

  1. Write your own code that fires up 100 and then each time one finishes, it fires up the next one.

使用 Bluebird 的 Promise.map() 具有内置的并发功能,可以管理同时进行的数量.

Use Bluebird's Promise.map() which has a built-in concurrency feature that will manage how many are inflight at the same time.

使用 Async 的 async.mapLimit() 具有内置的并发功能,可以管理同时进行的数量.

Use Async's async.mapLimit() which has a built-in concurrency feature that will manage how many are inflight at the same time.

至于自己编写代码来做到这一点,你可以这样做;

As for writing code yourself to do this, you could do something like this;

function fetchAll() {
    var start = 1;
    var end = 1000000;
    var concurrentMax = 100;
    var concurrentCnt = 0;
    var cntr = start;
    return new Promise(function(resolve, reject) {

        // start up requests until the max concurrent requests are going
        function run() {
            while (cntr < end && concurrentCnt < concurrentMax) {
                ++concurrentCnt;
                fetchData(cntr++).then(function() {
                    --concurrentCnt;
                    run();
                }, function(err) {
                    --concurrentCnt;
                    // decide what to do with error here
                    // to continue processing more requests, call run() here
                    // to stop processing more requests, call reject(err) here
                });
            }
            if (cntr >= end && concurrentCnt === 0) {
                // all requests are done here
                resolve();
            }        
        }

        run();
    });

}

这篇关于Node.js 套接字解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆