持久 TCP 连接、长时间超时和 IP 跳跃移动设备
我们有一个通过 HTTP 进行长轮询方案的应用程序(尽管这个问题可能适用于任何基于 TCP 的协议)。我们的超时时间相当长,大约 30 分钟。
我们有时会看到移动设备相当频繁地从一个 IP 跳转到另一个 IP,大约每分钟一次,这会导致服务器上堆积数十个长期存在的套接字。忍不住认为这会造成不必要的负载。
因此,我猜测某些 IP 网关在设备跳出时关闭连接方面比其他网关更好。我能想到的处理这个问题的策略是:
- 减少超时(增加设备的电池寿命)
- 当用户重新连接时关闭最后一个活动连接(需要 cookie 或用户 ID 跟踪)
还有其他策略吗?
We have an app with a long polling scheme over HTTP (although this question could apply to any TCP-based protocol). Our timeout is fairly high, 30 minutes or so.
What we see sometimes is mobile devices hop from IP to IP fairly often, every minute or so, and this causes dozens of long-lived sockets to pile up on the server. Can't help but think this is causing more load than neccessary.
So I am guessing that some IP gateways are better than others at closing connections when a device hops off. The strategies I can think of to deal with this are:
- Decrease the timeout (increasing battery life on the device)
- Close the last active connection when a user reconnects (requires cookie or user ID tracking)
Any others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑使用服务器中的 cookie 或某种 ID 来关闭最后一个活动连接。是的,这需要更多工作,但是一旦用户跳转地址,您就可以找到旧套接字并清理资源。绑定到用户名或类似的东西应该相当容易。
即使用户设备没有跳跃地址,您也可能遇到的另一个问题是,某些移动网络(也许您自己的网络)可能有一个状态防火墙,它将清理未使用的套接字,这将导致连接问题,因为新连接将再次需要 syn/syn-ack。如果您发现连接问题,请记住一些事情。
如果您确实决定玩 keepalis,请不要太激进,聊天应用程序是移动网络的瘟疫,当网络与服务器失去连接时,那些对网络造成影响的应用程序可能会导致网络出现各种问题(如果运营商流行起来的话,还有你)。至少有一种后退机制来重试连接,甚至可能尝试找出设备每分钟切换 IP 地址的原因。如果运行正常,这种情况就不会发生。
***我在加拿大的一家移动运营商工作,但是,我的评论并不反映我雇主的立场。
I would look into closing the last active connection using a cookie or some sort of ID in you're server. Yes it's more work, but as soon as the user hops addresses, you can find the old socket and clean up the resources right of way. It should be fairly easy to tie to a username or something like that.
The other problem you may run into even if the user equipment isn't hopping addresses, some mobile networks and maybe you're own network may have a statefull firewall that will clean up unused sockets, which will cause connectivity problems since a new connection will require the syn/syn-ack again. Just something to keep in mind if you're noticing connectivity problems.
If you do decide to play with keep alives, please don't be too aggressive, chatty applications are the plague of Mobile networks, and ones that hammer the network when it lose connection to the server can cause all sorts of problems for the network (and you if the carrier catches on). Atleast have a sort of backoff mechanism to retrying connectivity, and maybe even try to find out why the device is switching IP addresses every minute. If it's functioning properly that shouldn't occur.
***I work for a mobile operator in Canada, however, my comments do not reflect the position of my employer.
如果可以的话,打开套接字上的 TCP keepalive,并给它们一个相当低的计时器(例如,每 1-5 分钟一次)。只要您从套接字读取数据,您就能更快地检测到无法访问的对等点,并且与减少 30 分钟的应用程序超时相比,手机上的资源利用率更少。
If you can, turn on TCP keepalive on the sockets, and give them a fairly low timer (e.g. every 1-5 minute). As long as you're reading from the socket, you'll detect an unreachable peer faster - and with less resources utiilization on the phone than decreasing your 30 minute application timeout.