HTTP Keep Alive 时长与 TCP 超时时长的关系

发布于 2024-08-30 16:48:26 字数 437 浏览 6 评论 0 原文

我试图了解 TCP/IP 和 HTTP 超时值之间的关系。这两个超时值是不同还是相同?大多数 Web 服务器允许用户通过某些配置来设置 HTTP Keep Alive 超时值。 Web 服务器如何使用该值?该值是否只是在底层 TCP/IP 套接字上设置,即 HTTP 保持活动超时和 TCP/IP 保持活动超时是否相同?或者他们受到不同的对待?

我的理解是(可能不正确): 无论配置的 HTTP Keep Alive 超时如何,Web 服务器都使用底层 TCP 套接字上的默认超时(即无限期),并创建一个工作线程来倒计时指定的 HTTP 超时间隔。当工作线程达到零时,它会关闭连接。

编辑: 我的问题是关于两个超时持续时间之间的关系或差异,即当 HTTP 保持活动超时持续时间和 Web 服务器使用的 Socket 超时(SO_TIMEOUT)不同时会发生什么?我是否应该担心这两个是否相同?

I am trying to understand the relation between TCP/IP and HTTP timeout values. Are these two timeout values different or same? Most Web servers allow users to set the HTTP Keep Alive timeout value through some configuration. How is this value used by the Web servers? is this value just set on the underlying TCP/IP socket i.e is the HTTP Keep Alive timeout and TCP/IP Keep Alive Timeout same? or are they treated differently?

My understanding is (maybe incorrect):
The Web server uses the default timeout on the underlying TCP socket (i.e. indefinite) regardless of the configured HTTP Keep Alive timeout and creates a Worker thread that counts down the specified HTTP timeout interval. When the Worker thread hits zero, it closes the connection.

EDIT:
My question is about the relation or difference between the two timeout durations i.e. what will happen when HTTP keep-alive timeout duration and the timeout on the Socket (SO_TIMEOUT) which the Web server uses is different? should I even worry about these two being same or not?

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

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

发布评论

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

评论(4

静谧 2024-09-06 16:48:26

开放的 TCP 套接字不需要两方(我们称其为 Alice 和 Bob)之间进行任何通信,除非正在发送实际数据。如果 Alice 已收到对她发送给 Bob 的所有数据的确认,则她无法区分以下情况:

  1. Bob 已拔掉插头,或者 Alice 无法访问 Bob。
  2. Bob 已重新启动,或者忘记了他与 Alice 建立的开放 TCP 套接字。
  3. Bob 已与 Alice 连接,并且知道他有一个开放的连接,但没有任何想说的。

如果 Alice 一段时间没有收到 Bob 的消息并且想要区分上述情况,她可以重新发送最后一个数据字节,将其封装在合适的 TCP 帧中以便可识别为重传,本质上是假装她没有听到确认。如果鲍勃拔掉插头,即使她在几秒钟内重复发送数据包,她也不会听到任何回复。如果 Bob 重新启动或忘记连接,他会立即回复说连接无效。如果鲍勃对连接感到满意并且无话可说,他将通过确认重传进行响应。

超时指示当 Alice 发送需要回复的数据包时她愿意等待响应的时间。 Keepalive 时间指示在她重新传输最后一位数据并要求确认之前应允许等待多长时间。如果 Bob 失踪,Keepalive 和 Timeout 值的总和将指示 Alice 收到最后一位数据和她判定 Bob 已死亡之间的最坏情况时间。

An open TCP socket does not require any communication whatsoever between the two parties (let's call them Alice and Bob) unless actual data is being sent. If Alice has received acknowledgments for all the data she's sent to Bob, there's no way she can distinguish among the following cases:

  1. Bob has been unplugged, or is otherwise inaccessible to Alice.
  2. Bob has been rebooted, or otherwise forgotten about the open TCP socket he'd established with Alice.
  3. Bob is connected to Alice, and knows he has an open connection, but doesn't have anything he wants to say.

If Alice hasn't heard from Bob in awhile and wants to distinguish among the above conditions, she can resend her last byte of data, wrapped in a suitable TCP frame to be recognizable as a retransmission, essentially pretending she hasn't heard the acknowledgment. If Bob is unplugged, she'll hear nothing back, even if she repeatedly sends the packet over a period of many seconds. If Bob has rebooted or forgotten the connection, he will immediately respond saying the connection is invalid. If Bob is happy with the connection and simply has nothing to say, he'll respond with an acknowledgment of the retransmission.

The Timeout indicates how long Alice is willing to wait for a response when she sends a packet which demands a reply. The Keepalive time indicates how much time she should allow to lapse before she retransmits her last bit of data and demands an acknowledgment. If Bob goes missing, the sum of the Keepalive and Timeout values will indicate the worst-case time between Alice receiving her last bit of data and her deciding that Bob is dead.

绝不服输 2024-09-06 16:48:26

它们是两个独立的机制;这个名字是一个巧合。

HTTP 保持活动(也称为持久连接)使 TCP 套接字保持打开状态,以便可以在不建立新连接的情况下发出另一个请求。

TCP 保持活动是一种定期检查,以确保连接仍然正常运行。它通常用于确保 NAT 盒(例如 DSL 路由器)不会“忘记”内部和外部 IP/端口之间的映射。

They're two separate mechanisms; the name is a coincidence.

HTTP keep-alive (also known as persistent connections) is keeping the TCP socket open so that another request can be made without setting up a new connection.

TCP keep-alive is a periodic check to make sure that the connection is still up and functioning. It's often used to assure that a NAT box (e.g., a DSL router) doesn't "forget" the mapping between an internal and external ip/port.

七分※倦醒 2024-09-06 16:48:26

KeepAliveTimeout指令

描述:服务器等待后续操作的时间
持久连接上的请求语法:KeepAliveTimeout 秒
默认值:KeepAliveTimeout 15 上下文:服务器配置、虚拟主机
状态:核心模块:核心 Apache 将等待的秒数
关闭连接之前的后续请求。一旦请求有
已收到,Timeout指令指定的超时值
适用。

将 KeepAliveTimeout 设置为较高的值可能会导致性能下降
重载服务器中的问题。超时时间越高,
服务器进程将保持占用状态,等待与
闲置客户。

在基于名称的虚拟主机上下文中,第一个定义的值
一组NameVirtualHost中的虚拟主机(默认主机)将是
用过的。其他值将被忽略。

超时指令

描述:服务器等待某些事件的时间
请求失败之前语法:TimeOut 秒默认值:TimeOut 300
上下文:服务器配置、虚拟主机状态:核心模块:核心
TimeOut 指令当前定义了 Apache 将执行的时间量
等待三件事:

接收 GET 请求所需的总时间。量
收到 POST 或 PUT 请求上的 TCP 数据包之间的时间。这
TCP 数据包传输中 ACK 之间的时间量
回应。我们计划在某些时候将这些单独配置
指向路的下方。 1.2之前定时器默认为1200,但是
已降低至 300,这在大多数情况下仍远远超出所需
情况。默认情况下它不会设置得更低,因为可能仍然存在
代码中的奇怪位置在数据包发送时计时器不会重置
已发送。

KeepAliveTimeout Directive

Description: Amount of time the server will wait for subsequent
requests on a persistent connection Syntax: KeepAliveTimeout seconds
Default: KeepAliveTimeout 15 Context: server config, virtual host
Status: Core Module: core The number of seconds Apache will wait for a
subsequent request before closing the connection. Once a request has
been received, the timeout value specified by the Timeout directive
applies.

Setting KeepAliveTimeout to a high value may cause performance
problems in heavily loaded servers. The higher the timeout, the more
server processes will be kept occupied waiting on connections with
idle clients.

In a name-based virtual host context, the value of the first defined
virtual host (the default host) in a set of NameVirtualHost will be
used. The other values will be ignored.

TimeOut Directive

Description: Amount of time the server will wait for certain events
before failing a request Syntax: TimeOut seconds Default: TimeOut 300
Context: server config, virtual host Status: Core Module: core The
TimeOut directive currently defines the amount of time Apache will
wait for three things:

The total amount of time it takes to receive a GET request. The amount
of time between receipt of TCP packets on a POST or PUT request. The
amount of time between ACKs on transmissions of TCP packets in
responses. We plan on making these separately configurable at some
point down the road. The timer used to default to 1200 before 1.2, but
has been lowered to 300 which is still far more than necessary in most
situations. It is not set any lower by default because there may still
be odd places in the code where the timer is not reset when a packet
is sent.

沉溺在你眼里的海 2024-09-06 16:48:26

没有关系。
HTTP 保持活动状态是指自上次数据传输以来连接保持打开状态的时间。例如,如果保持活动时间为 15 秒,并且服务器在 NOW 向客户端发送数据,然后没有通过连接发送数据,则在 NOW + 15 秒后,任何一方都可以关闭连接。
TCP 级别是否存在空闲超时?不! TCP 套接字将永远保持活动状态。但服务器确实实现了自己的 TCP 空闲超时以避免内存泄漏。

There is no relationship.
HTTP keep alive is the time connection is kept open since last data has been transmitted. For example, if keep alive is 15 seconds, and server sends data to client at NOW, and then no data is sent over connection, then after NOW + 15 seconds, connection can be closed by either side.<br.
TCP timeout is connected to retry. If a packet is sent and not acknowledged, then after a TCP timeout. the sender can retry to send the packet. This ensures TCP is reliable. An ID connected to packet is used by receiver to deduplicate.
Is there an idle timeout at TCP level? NO! TCP sockets will remain alive forever. But servers do implement their own TCP idle timeout for avoiding memory leaks.

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