[英]Node.js - what happens if I call response.end while I/O and callbacks are still running?
In Node.js, what happens if call "response.end()" while my I/O calls and/or callbacks are still executing?在 Node.js 中,如果在我的 I/O 调用和/或回调仍在执行时调用“response.end()”会发生什么? As in the following:
如下所示:
var app = http.createServer(function(request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain'});
fs.writeFile('baz', 'contents', function() {
myOtherFunc();
response.end('Second response.end');
});
response.end('First response.end');
});
Specifically:具体来说:
Calling response.end doesn't stop any asynchronous code that's still executing.调用 response.end 不会停止任何仍在执行的异步代码。 You'll might see weird behavior though if you try to end or modify a response that's already been ended.
但是,如果您尝试结束或修改已经结束的响应,您可能会看到奇怪的行为。 So, basically yes to all.
所以,基本上都是肯定的。 I'll admit though, I'm not an expert on how node handles its HTTP connections behind the scenes.
不过我承认,我不是节点如何在幕后处理其 HTTP 连接的专家。
I haven't tested, but:我没有测试过,但是:
response.end
is called, but I doubt you can rely on that... at some point the connection must close and trying to send new data will "at best" fail silently.response.end
之前可能不会释放流,但我怀疑您是否可以依赖它...在某些时候连接必须关闭并尝试发送新数据将“充其量”无声地失败。response
will be gone if you don't get to it by the next few ticks.response
那么很可能就会消失。 Edit: I'm working with the assumption that the response
stream is closed explicitly, with the end
call, rather than just sitting out there waiting to be garbage collected.编辑:我假设
response
流在end
调用时显式关闭,而不是坐在那里等待垃圾收集。 My assumption is it's just done asynchronously rather than waiting on completion to continue on, and if you get there within the next couple ticks of the event loop, it may still be allocated.我的假设是它只是异步完成,而不是等待完成才能继续,如果您在事件循环的下几个滴答声中到达那里,它仍然可能被分配。
Edit Again: Your intrepid answerer has searched tirelessly through the node source and confirmed that, indeed, two calls to end
should indeed not work, the second should be short circuited by the OutgoingMessage.finished
property (see lines 499-501 and 541)再次编辑:您勇敢的回答者不知疲倦地搜索了节点源并确认,实际上,两个
end
调用确实应该不起作用,第二个调用应该被OutgoingMessage.finished
属性短路(参见第 499-501 和 541 行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.