linux - Haproxy 中大量的 TIME_WAIT

标签 linux http tcp haproxy

我们将 haproxy 1.3.26 托管在配备 2.13 GHz Intel Xeon 处理器的 CentOS 5.9 机器上,该处理器充当众多服务的 http 和 tcp 负载均衡器,峰值吞吐量约为 2000 个请求/秒。它已经运行了 2 年,但流量和服务数量都在逐渐增加。

我们观察到,即使在重新加载后,旧的 haproxy 进程仍然存在。在进一步调查中,我们发现旧进程有许多处于 TIME_WAIT 状态的连接。我们还看到 netstatlsof 花费了很长时间。关于引用http://agiletesting.blogspot.in/2013/07/the-mystery-of-stale-haproxy-processes.html我们引入了 option forceclose,但它扰乱了各种监控服务,因此将其还原。在进一步挖掘中,我们意识到在 /proc/net/sockstat 中,接近 200K 套接字处于 tw (TIME_WAIT) 状态,这令人惊讶,如/etc/haproxy/haproxy.cfg maxconn 已指定为 31000,ulimit-n 已指定为 64000。我们有 timeout servertimeout client300s 我们改成了 30s 但用处不大。

现在的疑问是:-

最佳答案

注意:本回答中的引号均来自a mail by Willy Tarreau (HAProxy 的主要作者)到 HAProxy 邮件列表。

处于TIME_WAIT 状态的连接是无害的,不再真正消耗任何资源。它们由内核在服务器上保存一段时间,以应对连接关闭后仍然收到包的罕见事件。关闭的连接在该状态下保持的默认时间通常为 120 秒(或最大段生命周期的 2 倍)

TIME_WAIT are harmless on the server side. You can easily reach millions without any issues.

如果您仍想减少该数量以更早地释放连接,您可以指示内核这样做。例如将其设置为 30 秒执行此操作:

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout

如果你有很多连接(无论是否在 TIME_WAIT 中),netstatlsofipcs 的性能都非常差,实际上会减慢整个过程系统宕机。再次引用威利的话:

There are two commands that you must absolutely never use in a monitoring system :

  • netstat -a
  • ipcs -a

Both of them will saturate the system and considerably slow it down when something starts to go wrong. For the sockets you should use what's in /proc/net/sockstat. You have all the numbers you want. If you need more details, use ss -a instead of netstat -a, it uses the netlink interface and is several orders of magnitude faster.

在 Debian 和 Ubuntu 系统上,ssiprouteiproute2 包中可用(取决于您的发行版本)。

关于linux - Haproxy 中大量的 TIME_WAIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421705/

相关文章:

Python:如何访问 Linux 路径

从文件名计算字数统计程序中的字数

java - Tomcat 容器中应用程序之间通信的最佳解决方案是什么?

python - Python请求-临时端口耗尽

linux - 使用 xmllint 从 XML 节点获取 URL,添加新行

linux - 带文件路径参数的 Windows 10 Bash 和 Google Chrome

http - 我可以模拟来自一台客户端机器的重 http 负载吗

http - 使用具有相同值的重复响应 header 可以吗?

c++ - 用于构建 Web 协议(protocol)的资源

.net - BinaryReader ReadByte() 是否超时?