服务器端的许多 TIME_WAIT 成本是多少? [英] What is the cost of many TIME_WAIT on the server side?

查看:12
本文介绍了服务器端的许多 TIME_WAIT 成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个客户端与服务器建立了很多短期连接.

Let's assume there is a client that makes a lot of short-living connections to a server.

如果客户端关闭连接,客户端会有很多端口处于TIME_WAIT状态.由于客户端用完了本地端口,因此无法快速尝试新的连接.

If the client closes the connection, there will be many ports in TIME_WAIT state on the client side. Since the client runs out of local ports, it becomes impossible to make a new connection attempt quickly.

如果服务器关闭连接,我会在服务器端看到很多TIME_WAIT.然而,这有什么害处吗?客户端(或其他客户端)可以继续尝试连接,因为它永远不会用完本地端口,并且 TIME_WAIT 状态的数量将在服务器端增加.最终会发生什么?有什么不好的事情发生吗?(减速、崩溃、断开连接等)

If the server closes the connection, I will see many TIME_WAITs on the server side. However, does this do any harm? The client (or other clients) can keep making connection attempts since it never runs out of local ports, and the number of TIME_WAIT state will increase on the server side. What happens eventually? Does something bad happen? (slowdown, crash, dropped connections, etc.)

请注意,我的问题不是TIME_WAIT 的目的是什么?"但是如果服务器上有这么多 TIME_WAIT 状态会怎样?"我已经知道在 TCP/IP 中关闭连接时会发生什么以及为什么需要 TIME_WAIT 状态.我不是想解决它,只是想知道它的潜在问题是什么.

Please note that my question is not "What is the purpose of TIME_WAIT?" but "What happens if there are so many TIME_WAIT states on the server?" I already know what happens when a connection is closed in TCP/IP and why TIME_WAIT state is required. I'm not trying to trouble-shoot it but just want to know what is the potential issue with it.

简单地说,让我们说 netstat -nat |grep :8080 |grep TIME_WAIT |wc -l 打印 100000.会发生什么?操作系统的网络堆栈会变慢吗?打开的文件太多"错误?或者,没有什么可担心的?

To put simply, let's say netstat -nat | grep :8080 | grep TIME_WAIT | wc -l prints 100000. What would happen? Does the OS's network stack slow down? "Too many open files" error? Or, just nothing to worry about?

推荐答案

TIME_WAIT 中的每个套接字都会消耗内核中的一些内存,通常比 ESTABLISHED 套接字要少一些仍然很重要.足够大的数字可能会耗尽内核内存,或者至少会降低性能,因为该内存可以用于其他目的.TIME_WAIT 套接字不保存打开的文件描述符(假设它们已正确关闭),因此您不必担心打开的文件过多"错误.

Each socket in TIME_WAIT consumes some memory in the kernel, usually somewhat less than an ESTABLISHED socket yet still significant. A sufficiently large number could exhaust kernel memory, or at least degrade performance because that memory could be used for other purposes. TIME_WAIT sockets do not hold open file descriptors (assuming they have been closed properly), so you should not need to worry about a "too many open files" error.

套接字还绑定了特定的 src/dst IP 地址和端口,因此在 TIME_WAIT 间隔期间不能重复使用.(这是 TIME_WAIT 状态的预期目的.)除非您需要使用相同的端口对重新连接端口,否则绑定端口通常不是问题.大多数情况下,一侧将使用临时端口,只有一侧锚定到众所周知的端口.但是,如果您在相同的两个 IP 地址之间反复频繁地连接,那么大量的 TIME_WAIT 套接字可能会耗尽临时端口空间.请注意,这只影响这个特定的 IP 地址对,不会影响与其他主机建立连接.

The socket also ties up that particular src/dst IP address and port so it cannot be reused for the duration of the TIME_WAIT interval. (This is the intended purpose of the TIME_WAIT state.) Tying up the port is not usually an issue unless you need to reconnect a with the same port pair. Most often one side will use an ephemeral port, with only one side anchored to a well known port. However, a very large number of TIME_WAIT sockets can exhaust the ephemeral port space if you are repeatedly and frequently connecting between the same two IP addresses. Note this only affects this particular IP address pair, and will not affect establishment of connections with other hosts.

这篇关于服务器端的许多 TIME_WAIT 成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆