Node.js 中的 setKeepAlive 是如何工作的以及如何实现?

发布于 2024-12-02 20:46:21 字数 69 浏览 1 评论 0原文

无法理解 Node.js 网络套接字中 setKeepAlive 方法的原理。 initialDelay 完成后会发生什么?

Cannot understand the philosophy of setKeepAlive method in Node.js' net sockets. What happens after initialDelay finishes?

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

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

发布评论

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

评论(1

浪漫之都 2024-12-09 20:46:21

此方法控制底层 TCP 套接字上的 TCP 保持活动功能。有关 TCP Keepalive 的信息,请查看本文。下面的代码片段解释了 initialDelay(“keepalive 计时器”)的作用:

2.1。什么是 TCP 保活?

keepalive 的概念非常简单:当您建立 TCP 连接时,您会关联一组计时器。其中一些定时器处理保活过程。当保活定时器达到零时,您向对等方发送一个保活探测数据包,其中没有数据,并且 ACK 标志打开。由于 TCP/IP 规范,您可以这样做,作为一种重复的 ACK,并且远程端点将没有参数,因为 TCP 是面向流的协议。另一方面,您将收到来自远程主机的回复(根本不需要支持 keepalive,只需要 TCP/IP),没有数据和 ACK 集。

如果您收到对 keepalive 探测的回复,您可以断言连接仍然正常运行,而无需担心用户级实现。事实上,TCP 允许您处理流,而不是数据包,因此零长度数据包对于用户程序来说并不危险。

此过程很有用,因为如果其他对等点失去连接(例如通过重新启动),您会注意到连接已断开,即使您没有流量。如果您的对等方没有回复保活探测,您可以断言该连接不能被视为有效,然后采取正确的操作。

This method controls TCP keep-alive functionality on the underlying TCP socket. Check out this article for information on TCP Keepalive. Here's a snippet that explains what initialDelay (the "keepalive timer") does:

2.1. What is TCP keepalive?

The keepalive concept is very simple: when you set up a TCP connection, you associate a set of timers. Some of these timers deal with the keepalive procedure. When the keepalive timer reaches zero, you send your peer a keepalive probe packet with no data in it and the ACK flag turned on. You can do this because of the TCP/IP specifications, as a sort of duplicate ACK, and the remote endpoint will have no arguments, as TCP is a stream-oriented protocol. On the other hand, you will receive a reply from the remote host (which doesn't need to support keepalive at all, just TCP/IP), with no data and the ACK set.

If you receive a reply to your keepalive probe, you can assert that the connection is still up and running without worrying about the user-level implementation. In fact, TCP permits you to handle a stream, not packets, and so a zero-length data packet is not dangerous for the user program.

This procedure is useful because if the other peers lose their connection (for example by rebooting) you will notice that the connection is broken, even if you don't have traffic on it. If the keepalive probes are not replied to by your peer, you can assert that the connection cannot be considered valid and then take the correct action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文