首页 > 解决方案 > 在 end() 之后销毁套接字有什么影响?

问题描述

我看到的情况是clientError,客户端不确认 FIN,即

httpServer.on('clientError', (error, socket) => {
    socket.write(stringifyHttpResponseMessage(400, {connection: 'close'}, 'OK'));

    socket.end();

    setTimeout(() => {
        // socket remains writable.
        // What is the risk of calling `socket.destroy()` here?

        socket.destroy();
    }, 1000);
});

结果,一连串的客户端错误导致打开套接字,直到达到相关的空闲套接字超时(这是相当高的)。

之后销毁套接字有什么影响end()

标签: node.jshttp

解决方案


我面临着同样的问题。为了解决这个问题,我在响应写入后销毁了套接字,而不是结束套接字。这是我所做的一个例子。

httpServer.on('clientError', (error, socket) => {
  socket.write(stringifyHttpResponseMessage(400, {connection: 'close'}, 'OK'), 'utf8', () => {
    socket.destroy();
  });
});

到目前为止,它按预期工作。


推荐阅读