【发布时间】: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 个浏览器选项卡来加载以下网址 - 按顺序而不等待任何响应:
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 请求怎么办?发回你的发现。
-
谢谢。我已经意识到浏览器能够延迟 - 可能是一个小的延迟来评估它是否可以缓存第一个请求并将其用于第二个或其他。无论如何,我已经在下面发布了答案。干杯