节点Restify不响应对同一端点的连续请求

时间:2015-03-11 13:27:28

标签: node.js restify

我一直在玩耍,并且遇到了一种我很难理解的行为。

我的代码如下:

var restify = require('restify');
var logger = require('log4js').getLogger('index.js');

var server = restify.createServer();

server.listen(3000)

var helloCB = function (request, response, next) {

    logger.info('got new request', request.url)
    setTimeout(function () {

        logger.info('sending response', request.url)
        response.send('hello')
        next(false)
    }, 60000)
}


server.get('/hello', helloCB);

现在,如果我通过连续打开 3个浏览器标签以及以下每个网址来加载以下网址,并且无需等待任何响应:

  

http://localhost:3000/hello

     

http://localhost:3000/hello

     

http://localhost:3000/hello?1

Restify似乎只是将请求排队到同一个端点。我的应用程序的日志如下:

[2015-03-11 14:17:57.601] [INFO] index.js - got new request /hello
[2015-03-11 14:18:02.299] [INFO] index.js - got new request /hello?1
[2015-03-11 14:19:57.603] [INFO] index.js - got new request /hello

请注意,第二个请求实际上是最后记录的,并在请求后2分钟左右记录。

作为辅助测试,我尝试使用ab工具模拟类似的测试:

  

ab -n 5 -c 2 -k http://localhost:3000/hello

我得到了以下日志(这实际上是使用较小的超时来发送响应):

[2015-03-11 14:23:51.883] [INFO] index.js - got new request /hello
[2015-03-11 14:23:51.887] [INFO] index.js - got new request /hello
[2015-03-11 14:23:57.890] [INFO] index.js - sending response /hello
[2015-03-11 14:23:57.901] [INFO] index.js - sending response /hello
[2015-03-11 14:23:57.902] [INFO] index.js - got new request /hello
[2015-03-11 14:23:57.902] [INFO] index.js - got new request /hello
[2015-03-11 14:24:03.904] [INFO] index.js - sending response /hello
[2015-03-11 14:24:03.905] [INFO] index.js - sending response /hello
[2015-03-11 14:24:03.906] [INFO] index.js - got new request /hello
[2015-03-11 14:24:09.910] [INFO] index.js - sending response /hello

知道为什么在第一次测试时对同一端点的请求似乎排队而不是立即处理?

由于

1 个答案:

答案 0 :(得分:1)

经过进一步调查后,我意识到以下几点:

  • 所有请求实际上都是由浏览器执行的(chrome:// net-internals /#events)
  • 使用curl
  • 无法重现接收请求的延迟
  • Charles代理显示请求实际上是按照restify收到的顺序执行的

所以浏览器应该受到指责。另外,我也可以在FF中重现这一点。