对于不耐烦的人:

如何使用setsockopt(),ioctl()等更改Linux中单个连接的/ proc / sys / net / ipv4 / tcp_retries2的值,或者是否可能?

更长的描述:

我正在开发一个使用长轮询HTTP请求的应用程序。在服务器端,当客户端关闭连接时,需要知道它。准确性并不重要,但肯定不会超过15分钟。更接近一分钟就行了。

对于不熟悉这个概念的人来说,长时间的轮询HTTP请求如下:

>客户端发送请求

>服务器使用HTTP标头进行响应,但保留响应。使用分块传输编码,允许服务器在数据可用时发送数据位。

>当发送所有数据时,服务器发送一个“关闭块”,表示响应完成。

在我的应用程序中,服务器会立即向客户端发送“心跳”(默认为30秒)。心跳只是作为响应块发送的换行符。这意味着保持线路忙,以便我们通知连接丢失。

当客户端正确关闭时,没有问题。但是当它被强制关闭时(例如客户端计算机掉电),则不会发送TCP复位。在这种情况下,服务器发送心跳,客户端不发送ACK。此后,服务器在放弃并向应用层(我们的HTTP服务器)报告故障后,持续重传数据包大约15分钟。在我的情况下,15分钟太久了。

我可以通过写入/ proc / sys / net / ipv4 /中的以下文件来控制重发时间:

tcp_retries1 - INTEGER

This value influences the time, after which TCP decides, that

something is wrong due to unacknowledged RTO retransmissions,

and reports this suspicion to the network layer.

See tcp_retries2 for more details.

RFC 1122 recommends at least 3 retransmissions, which is the

default.

tcp_retries2 - INTEGER

This value influences the timeout of an alive TCP connection,

when RTO retransmissions remain unacknowledged.

Given a value of N, a hypothetical TCP connection following

exponential backoff with an initial RTO of TCP_RTO_MIN would

retransmit N times before killing the connection at the (N+1)th RTO.

The default value of 15 yields a hypothetical timeout of 924.6

seconds and is a lower bound for the effective timeout.

TCP will effectively time out at the first RTO which exceeds the

hypothetical timeout.

RFC 1122 recommends at least 100 seconds for the timeout,

which corresponds to a value of at least 8.

tcp_retries2的默认值确实是8,我的15分钟(900秒)的重传体验符合上面引用的内核文档。

例如,如果我将tcp_retries2的值更改为5,那么连接会更快地消失。但是这样设置会影响系统中的所有连接,而且我真的只想设置一个长的轮询连接。

RFC 1122的引用:

4.2.3.5 TCP Connection Failures

Excessive retransmission of the same segment by TCP

indicates some failure of the remote host or the Internet

path. This failure may be of short or long duration. The

following procedure MUST be used to handle excessive

retransmissions of data segments [IP:11]:

(a) There are two thresholds R1 and R2 measuring the amount

of retransmission that has occurred for the same

segment. R1 and R2 might be measured in time units or

as a count of retransmissions.

(b) When the number of transmissions of the same segment

reaches or exceeds threshold R1, pass negative advice

(see Section 3.3.1.4) to the IP layer, to trigger

dead-gateway diagnosis.

(c) When the number of transmissions of the same segment

reaches a threshold R2 greater than R1, close the

connection.

(d) An application MUST be able to set the value for R2 for

a particular connection. For example, an interactive

application might set R2 to "infinity," giving the user

control over when to disconnect.

(e) TCP SHOULD inform the application of the delivery

problem (unless such information has been disabled by

the application; see Section 4.2.4.1), when R1 is

reached and before R2. This will allow a remote login

(User Telnet) application program to inform the user,

for example.

在我看来,Linux中的tcp_retries1和tcp_retries2对应于RFC中的R1和R2。 RFC明确指出(在d)中,一致的实现必须允许设置R2的值,但是我没有找到使用setsockopt(),ioctl()等方法。

另一个选择是在超过R1时收到通知(项目e)。这不如设置R2,尽管如此,我认为R1很快(几秒钟)就会被击中,并且每个连接都不能设置R1的值,或至少RFC不需要。

linux tcp项目,Linux上TCP重传的应用控制相关推荐

  1. linux系统优化项目,Linux之系统优化

    查看系统版本 [root@luffy- /]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@luffy- /]# uname -m ...

  2. linux学习项目,[ Linux运维学习 ] 路径及实战项目合集

    我们知道运维工程师(Operations)最基本的职责就是负责服务的稳定性并确保整个服务的高可用性,同时不断优化系统架构.提升部署效率.优化资源利用率,确保服务可以7*24H不间断地为用户提供服务. ...

  3. 【案例分析】Linux和Windows在TCP数据包上的差异+网闸兼容问题引发的TCP连接失败故障分析

    1.前言 出于项目保密原因,本文所提及的IP地址并非真实IP地址,其余技术细节均与案例实际环境相同,供学习交流参考. 2.案例背景 我们前段时间接到项目现场工程师电话,对方称他们新上架了一台Linux ...

  4. linux上TCP connection timeout的原因查找

    linux上TCP connection timeout的原因查找 好久没有写文章了, 今天解决了一个网络连接超时的问题, 记录以备查看. 最近在线上nginx经常出现输出connection tim ...

  5. linux服务器上tcp有大量time_wait状态的解决方法和原因解释

    在检查服务器时,发现有很多连接超时情况出现,用netstat命令查看,tcp的time_wait状态较多,需要进行优化. 1. 看一下现在time_wait的数量 netstat -an | grep ...

  6. codeblock socket 编译错误_从Linux源码看Socket(TCP)Client端的Connect

    从Linux源码看Socket(TCP)Client端的Connect 前言 笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码,是一件Exciting的事情. 今天笔者就来从Linux源码的 ...

  7. java linux 调用32位so_从linux源码看socket(tcp)的timeout

    从linux源码看socket(tcp)的timeout 前言 网络编程中超时时间是一个重要但又容易被忽略的问题,对其的设置需要仔细斟酌.在经历了数次物理机宕机之后,笔者详细的考察了在网络编程(tcp ...

  8. linux tcp连接计算机,计算机基础知识——linux socket套接字tcp连接分析

    2016.7.4 今天晚上对项目顶层文件(daemon)进行了分析,对其中的TCP连接进行具体的代码级分析. 1.需求分析 首先得知道我们这里为什么要用TCP连接,我们的整个测试系统是由上位机作为客户 ...

  9. Linux网络协议栈:一个TCP链接的耗时

    <一次系统调用开销到底有多大?strace.time.perf命令> 目录 一 正常TCP连接建立过程 二 TCP连接建立时的异常情况 1)客户端connect系统调用耗时失控 2)半/全 ...

最新文章

  1. 全国四分之三的光伏电站市场面临较大投资风险
  2. Delphi 的运算符重载(2) - 可重载运算符列表
  3. python不能分配给操作员_Python:无法分配给li
  4. 前端学习(2322):angular内容介绍
  5. modem建链过程详述
  6. 数据结构-排序基础代码
  7. 提升网络销售转化率的10种方法
  8. python数据分析之(7)简单绘图pylab
  9. 64位userdata.dll丢失_因为计算机中丢失OCI.dll尝试重新安装该程序以解决此问题
  10. 鸿蒙系统主题如何自定义,华为鸿蒙系统细节曝光 支持自定义选项
  11. 《穿越计算机的迷雾》读书笔记七
  12. 网站XSS跨站攻击脚本语法
  13. Android 通过usb调用高拍仪进行拍照
  14. 【DeeCamp 优秀项目详解】从零开始到 AI 技术落地,只用三周
  15. PDF.js使用教程
  16. JavaScript的20个工具函数助力H5高效开发
  17. ultraEdit打开新文件时,会提示:“文件可能不是doc格式,你要转换File为DOC
  18. 导出Fbx和obj的工具
  19. 平安科技2019校招后端工程师一面
  20. MPLAB X LAB初学使用

热门文章

  1. @Service注解的作用
  2. 1.6梯度提升的优化算法实现概况
  3. C程序范例(2)——学生管理系统”链表“实现
  4. UITabBarController加载之后不显示sub view controller
  5. EasyDarwin开源流媒体服务器如何实现按需推送直播的
  6. [HDU 3555] Bomb
  7. 如何在字符串中添加双引号?
  8. 通过javascript在IE模拟键盘输入
  9. 从头学习DirectDraw
  10. SpringBoot Scheduled Cron表达式范例记录