NodeJS HTTPServer 需要很长时间才能关闭

发布于 2024-12-25 07:19:01 字数 986 浏览 3 评论 0原文

我正在开发一个 Zappa 应用程序,目前正在尝试制作一个小监视脚本来停止服务器,清除 require.cache,然后在文件更改时重新要求并重新启动服务器,例如:

# watch all dependent files
for file of require.cache
  fs.watch file, ->
    # attach a handler to 'close'
    # -- here's the issue: this takes far too long to trigger
    server.on 'close', ->
      server = require './server'
      server.start()

      # log the new server's id
      console.log server.id

    # stop the current server instance
    server.stop()

    # clear require's cache
    delete require.cache[f] for f of require.cache

我的请求处理程序中还有一个 console.log server.id 行,以便我可以检查 ID 是否匹配。

所以,发生的情况是:当我更改依赖项时,服务器停止,新的服务器启动并记录新的 ID,这一切都是肉汁。但是,在之后的一段随机时间内,对服务器的请求仍会记录旧 ID,这表明旧侦听器仍以某种方式附加。最终,监听器似乎“切换”并记录了新的 ID。

更新:这似乎与 close 事件有关(毫不奇怪) - 如果我将一个简单的 console.log 'close' 回调附加到close事件,'close'出现后ID开始变化。但是,close 事件可能需要很长时间(10s+)才能触发,为什么会花这么长时间?

I'm working on a Zappa app, and I'm currently trying to make a little watch script that stops the server, clears require.cache then re-requires and restarts the server when a file changes, something like:

# watch all dependent files
for file of require.cache
  fs.watch file, ->
    # attach a handler to 'close'
    # -- here's the issue: this takes far too long to trigger
    server.on 'close', ->
      server = require './server'
      server.start()

      # log the new server's id
      console.log server.id

    # stop the current server instance
    server.stop()

    # clear require's cache
    delete require.cache[f] for f of require.cache

I also have a console.log server.id line in my request handler so I can check if the IDs match.

So, what happens is: when I change a dependency, the server stops, a new one starts and the new ID is logged, which is all gravy. However, for a random amount of time after, requests to the server still log the old ID, indicating that the old listener is still attached somehow. Eventually, the listener seems to 'switch over' and the new ID is logged.

Update: it seems this is related to the close event (unsurprisingly) - if I attach a simple console.log 'close' callback to the close event, the ID starts changing after 'close' appears. However, it can take a long time (10s+) for the close event to be fired, why might it take so long?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溇涏 2025-01-01 07:19:01

根据 node.js 文档

server.close()

停止服务器接受新连接。请参阅 net.Server.close()。

因此,您的服务器将停止接受新连接,但在当前连接关闭之前,它实际上不会关闭(并发出 close 事件)。我的猜测是您有客户端连接到它,可能有 keep-alive 请求。

According to the node.js docs:

server.close()

Stops the server from accepting new connections. See net.Server.close().

So, your server would stop accepting new connections, but it won't actually close (and emit close event) until current connections are closed. My guess is that you have clients connected to it, perhaps with keep-alive requests.

白云不回头 2025-01-01 07:19:01

我正在寻找同样的问题。为了给您和其他正在寻找此解决方案的人提供一个解决方案:

如果这不是您的项目中的问题,您可以立即关闭所有连接。这完全解决了我的关闭问题。

app.use((req, res, next) => {
    res.setHeader('Connection', 'close');
    next();
});

I was searching for the same problem. To give you and others searching for this a solution:

You can close all connections immediately, if this is not a problem in your project. This solves the closing problem for me completly.

app.use((req, res, next) => {
    res.setHeader('Connection', 'close');
    next();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文