节点 Restify 不响应对同一端点的连续请求
Node Restify not responding to consecutive requests to the same endpoint
我一直在玩 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 个浏览器选项卡来加载以下网址 - 按顺序且不等待任何响应:
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
知道为什么在第一次测试时,对同一端点的请求似乎在排队而不是立即处理吗?
谢谢
经过进一步调查,我意识到以下几点:
- 所有请求实际上都是由浏览器执行的 (chrome://net-internals/#events)
- 使用 curl 无法重现接收请求的延迟
- Charles 代理显示请求实际上是按照 restify 收到的顺序执行的
所以浏览器是罪魁祸首。附带说明一下,我也能够在 FF 中重现这一点。
我一直在玩 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 个浏览器选项卡来加载以下网址 - 按顺序且不等待任何响应:
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
知道为什么在第一次测试时,对同一端点的请求似乎在排队而不是立即处理吗?
谢谢
经过进一步调查,我意识到以下几点:
- 所有请求实际上都是由浏览器执行的 (chrome://net-internals/#events)
- 使用 curl 无法重现接收请求的延迟
- Charles 代理显示请求实际上是按照 restify 收到的顺序执行的
所以浏览器是罪魁祸首。附带说明一下,我也能够在 FF 中重现这一点。