【问题标题】:Node Restify not responding to consecutive requests to the same endpointNode Restify 不响应对同一端点的连续请求
【发布时间】:2015-03-12 06:45:18
【问题描述】:

我一直在玩 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

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

谢谢

【问题讨论】:

  • 可能是您的浏览器,或者重新“缓存”同一个 IP 正在请求同一个端点。我不确定,但我猜您的浏览器正在等待第一个返回,然后再发送第二个。如果您发送 3 个并发 cUrl 请求怎么办?发回你的发现。
  • 谢谢。我已经意识到浏览器能够延迟 - 可能是一个小的延迟来评估它是否可以缓存第一个请求并将其用于第二个或其他。无论如何,我已经在下面发布了答案。干杯

标签: node.js restify


【解决方案1】:

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

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

所以浏览器是罪魁祸首。在旁注中,我也能够在 FF 中重现这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2020-07-07
    • 2021-10-31
    • 1970-01-01
    • 2020-01-13
    • 2019-08-03
    • 2013-09-15
    相关资源
    最近更新 更多