使用Node.js + Express 2“代理”很多HTTP请求 [英] “Proxying” a lot of HTTP requests with Node.js + Express 2

查看:172
本文介绍了使用Node.js + Express 2“代理”很多HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js + Express中编写代理2.代理应该:


  1. 解密POST有效内容并向服务器发出HTTP请求基于结果;

  2. 加密从服务器的回复并发送回客户端。

加密相关部分工作正常。我遇到的问题是超时。代理应该在不到15秒内处理请求。其实大部分都是500ms以下。



当我增加并行请求数时出现问题。大多数请求都完成了,但是有些在15秒之后失败了+几毫米。 ab -n5000 -c300 可以正常工作,但并发性为500时,某些请求超时失败。



I只能推测,但似乎问题是一个回调的顺序。 ETIMEDOUT 可能要先挂起的请求,因为节点关注的最新的请求仍然在500ms以下的时间内处理。



PS:远程服务器没有问题。我正在使用请求进行互动。



upd



某些代码的工作原理如下:

  function queryRemote(req,res){
var options = {}; //基于req对象(URI,body,authorization等)构建
请求(options,function(err,httpResponse,body){
return err?send500(req,res)
:res.end(encrypt(body));
});
}

app.use(myBodyParser); //在有效负载
//中读取十六进制字符串,并在'end'事件上调用next()

app.post('/',[checkHeaders,//检查内容类型和授权标题
authUser,//查询数据库和调用next()
parseRequest],//解密有效负载,解析JSON,调用next()
函数(req,res){
req.socket.setTimeout(TIMEOUT);
queryRemote(req,res);
});

我的问题如下:当 ab 发行让我们说,20个POST到 / ,express路由处理程序被称为数千次。这并不总是发生,有时20个,只有20个请求被及时处理。



当然, ab 不是问题我100%肯定只有20个请求由 ab发送。但是路由处理程序被多次调用。



我找不到这样的行为的原因,任何建议?

解决方案

超时是由于使用 http .globalAgent ,默认情况下可以处理最多5个< a>并发请求到一个主机:port (这在我的情况下是不够的)。



请求(而不是数十)由 ab (Wireshark批准的事实在OS X下发送;我不能在Ubuntu内在Parallels中重现)。


I'm writing proxy in Node.js + Express 2. Proxy should:

  1. decrypt POST payload and issue HTTP request to server based on result;
  2. encrypt reply from server and send it back to client.

Encryption-related part works fine. The problem I'm facing is timeouts. Proxy should process requests in less than 15 secs. And most of them are under 500ms, actually.

Problem appears when I increase number of parallel requests. Most requests are completed ok, but some are failed after 15 secs + couple of millis. ab -n5000 -c300 works fine, but with concurrency of 500 it fails for some requests with timeout.

I could only speculate, but it seems thant problem is an order of callbacks exectuion. Is it possible that requests that comes first are hanging until ETIMEDOUT because of node's focus in latest ones which are still being processed in time under 500ms.

P.S.: There is no problem with remote server. I'm using request for interactions with it.

upd

The way things works with some code:

function queryRemote(req, res) {
  var options = {};  // built based on req object (URI, body, authorization, etc.)
  request(options, function(err, httpResponse, body) {
    return err ? send500(req, res)
               : res.end(encrypt(body));
  });
}

app.use(myBodyParser);  // reads hex string in payload
                        // and calls next() on 'end' event

app.post('/', [checkHeaders,   // check Content-Type and Authorization headers
               authUser,       // query DB and call next()
               parseRequest],  // decrypt payload, parse JSON, call next()
         function(req, res) {
  req.socket.setTimeout(TIMEOUT);
  queryRemote(req, res);
});

My problem is following: when ab issuing, let's say, 20 POSTs to /, express route handler gets called like thousands of times. That's not always happening, sometimes 20 and only 20 requests are processed in timely fashion.

Of course, ab is not a problem. I'm 100% sure that only 20 requests sent by ab. But route handler gets called multiple times.

I can't find reasons for such behaviour, any advice?

解决方案

Timeouts were caused by using http.globalAgent which by default can process up to 5 concurrent requests to one host:port (which isn't enough in my case).

Thouthands of requests (instead of tens) were sent by ab (Wireshark approved fact under OS X; I can not reproduce this under Ubuntu inside Parallels).

这篇关于使用Node.js + Express 2“代理”很多HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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