热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

gen_tcp参数总结

动机在用elixir写rpcserverclient时,需要对传入gen_tcp的参数做一些考量.如,部分参数应该允许用户修改,比如sndbufrecbuf,让用户根据使用场景调节
动机

在用elixir 写 rpc server/client时, 需要对传入gen_tcp的参数做一些考量. 如, 部分参数应该允许用户修改, 比如sndbuf recbuf, 让用户根据使用场景调节, 部分参数应该屏蔽, 减少使用理解成本.
故, 深挖了一下gen_tcp的option

代码版本

文章中贴的文件和行号来源于如下代码版本

  • erlang: OTP-21.0.9
options

Available options for tcp:connect

inet.erl:723

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Available options for tcp:connect
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
connect_options() ->
[tos, tclass, priority, reuseaddr, keepalive, linger, sndbuf, recbuf, nodelay,
header, active, packet, packet_size, buffer, mode, deliver, line_delimiter,
exit_on_close, high_watermark, low_watermark, high_msgq_watermark,
low_msgq_watermark, send_timeout, send_timeout_close, delay_send, raw,
show_econnreset, bind_to_device].

tos

type of service
下图来自tcp ip详解 卷1
《gen_tcp参数总结》

tclass

IPV6_TCLASS
{tclass, Integer}
Sets IPV6_TCLASS IP level options on platforms where this is implemented.
The behavior and allowed range varies between different systems.
The option is ignored on platforms where it is not implemented. Use with caution.
不知道具体含义, 忽略

priority

SO_PRIORITY
Set the protocol-defined priority for all packets to be sent
on this socket. Linux uses this value to order the networking
queues: packets with a higher priority may be processed first
depending on the selected device queueing discipline. Setting
a priority outside the range 0 to 6 requires the CAP_NET_ADMIN
capability.

reuseaddr

SO_REUSEPORT (since Linux 3.9)
Permits multiple AF_INET or AF_INET6 sockets to be bound to an
identical socket address. This option must be set on each
socket (including the first socket) prior to calling bind(2)
on the socket. To prevent port hijacking, all of the pro‐
cesses binding to the same address must have the same effec‐
tive UID. This option can be employed with both TCP and UDP
sockets.
For TCP sockets, this option allows accept(2) load distribu‐
tion in a multi-threaded server to be improved by using a dis‐
tinct listener socket for each thread. This provides improved
load distribution as compared to traditional techniques such
using a single accept(2)ing thread that distributes connec‐
tions, or having multiple threads that compete to accept(2)
from the same socket.
For UDP sockets, the use of this option can provide better
distribution of incoming datagrams to multiple processes (or
threads) as compared to the traditional technique of having
multiple processes compete to receive datagrams on the same
socket.

keepalive

SO_KEEPALIVE
Enable sending of keep-alive messages on connection-oriented
sockets. Expects an integer boolean flag.

keepalive的可选参数和含义

root@1ba6f31f7bc3:/# cat /proc/sys/net/ipv4/tcp_keepalive_time
1800
the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any furthe
root@1ba6f31f7bc3:/# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
root@1ba6f31f7bc3:/# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
the number of unacknowledged probes to send before considering the connection dead and notifying the application layer

主要问题:

  1. 没有穿透负载均衡器.
  2. 检测得太慢.
  3. 不知道应用状态.

linger

SO_LINGER
Sets or gets the SO_LINGER option. The argument is a linger
structure.
struct linger {
int l_onoff; /* linger active */
int l_linger; /* how many seconds to linger for */
};
When enabled, a close(2) or shutdown(2) will not return until
all queued messages for the socket have been successfully sent
or the linger timeout has been reached. Otherwise, the call
returns immediately and the closing is done in the background.
When the socket is closed as part of exit(2), it always
lingers in the background.

close/shutdown前是否等待所有包都送达.

sndbuf recbuf buffer

SO_SNDBUF
Sets or gets the maximum socket send buffer in bytes. The
kernel doubles this value (to allow space for bookkeeping
overhead) when it is set using setsockopt(2), and this doubled
value is returned by getsockopt(2). The default value is set
by the /proc/sys/net/core/wmem_default file and the maximum
allowed value is set by the /proc/sys/net/core/wmem_max file.
The minimum (doubled) value for this option is 2048.
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The
kernel doubles this value (to allow space for bookkeeping
overhead) when it is set using setsockopt(2), and this doubled
value is returned by getsockopt(2). The default value is set
by the /proc/sys/net/core/rmem_default file, and the maximum
allowed value is set by the /proc/sys/net/core/rmem_max file.
The minimum (doubled) value for this option is 256.

inet_drv.c:6708

case INET_OPT_SNDBUF:
{
arg.ival= get_int32 (curr); curr += 4;
proto = SOL_SOCKET;
type = SO_SNDBUF;
arg_ptr = (char*) (&arg.ival);
arg_sz = sizeof ( arg.ival);
/* Adjust the size of the user-level recv buffer, so it's not
smaller than the kernel one: */
if (desc->bufsz <= arg.ival)
desc->bufsz = arg.ival;
break;
}

可以看到, buffer是用户的缓存, 一定不小于内核buffer, 然而获得的buffer小于 recbuf, sdnbuf.
怀疑: 设置了recvbuf, sndbuf才会改变buffer.

nodelay

TCP_NODELAY

DISCUSSION:
The Nagle algorithm is generally as follows:
If there is unacknowledged data (i.e., SND.NXT >
SND.UNA), then the sending TCP buffers all user
data (regardless of the PSH bit), until the
outstanding data has been acknowledged or until
the TCP can send a full-sized segment (Eff.snd.MSS
bytes; see Section 4.2.2.6).
Some applications (e.g., real-time display window
updates) require that the Nagle algorithm be turned
off, so small data segments can be streamed out at the
maximum rate.

可以看到和延迟确认一起使用时会带来很大的延时.

header

http://erlang.org/doc/man/ine&#8230;
定长header, 处理定长header时可以一用.

active

用被动模式, 异步收发.

packet, raw

包头长度. 即用多少字节表示包长. raw 等同于 {packet, 0}

packet_size

包最大长度. 最大允许的包长.

mode

{mode, Mode :: binary | list}
Received Packet is delivered as defined by Mode.

deliver

{deliver, port | term}
When {active, true}, data is delivered on the form port : {S, {data, [H1,..Hsz | Data]}} or term : {tcp, S, [H1..Hsz | Data]}.

line_delimiter

{line_delimiter, Char}(TCP/IP sockets)
Sets the line delimiting character for line-oriented protocols (line). Defaults to $n.

exit_on_close

{exit_on_close, Boolean}
This option is set to true by default.
The only reason to set it to false is if you want to continue sending data to the socket after a close is detected, for example, if the peer uses gen_tcp:shutdown/2 to shut down the write side.

high_watermark, low_watermark, high_msgq_watermark,

low_msgq_watermark

影响socket busy state的切换.
需要搞清楚几个问题:
socket busy state是什么, 譬如调用发送/接收有什么返回?
msgq data size 和 socket data size, socket data size 是否就是buffer?

send_timeout

发送超时时间, 默认无限等待

send_timeout_close

发送超时是否自动关闭.

delay_send

应用层并包. 默认关闭. 可以考虑开启.

show_econnreset

是否把RST当正常关闭.

bind_to_device

使用指定的设备(网卡)

参考资料
  1. http://erlang.org/doc/man/gen&#8230;
  2. http://man7.org/linux/man-pag&#8230;
  3. http://erlang.org/doc/man/ine&#8230;
  4. https://github.com/erlang/otp
  5. https://tools.ietf.org/html/r&#8230;
  6. https://www.ietf.org/rfc/rfc3&#8230;
  7. https://tools.ietf.org/html/r&#8230;

推荐阅读
  • 深入剖析JVM垃圾回收机制
    本文详细探讨了Java虚拟机(JVM)中的垃圾回收机制,包括其意义、对象判定方法、引用类型、常见垃圾收集算法以及各种垃圾收集器的特点和工作原理。通过理解这些内容,开发人员可以更好地优化内存管理和程序性能。 ... [详细]
  • 2017-2018年度《网络编程与安全》第五次实验报告
    本报告详细记录了2017-2018学年《网络编程与安全》课程第五次实验的具体内容、实验过程、遇到的问题及解决方案。 ... [详细]
  • 本文探讨了如何通过一系列技术手段提升Spring Boot项目的并发处理能力,解决生产环境中因慢请求导致的系统性能下降问题。 ... [详细]
  • 使用WinForms 实现 RabbitMQ RPC 示例
    本文通过两个WinForms应用程序演示了如何使用RabbitMQ实现远程过程调用(RPC)。一个应用作为客户端发送请求,另一个应用作为服务端处理请求并返回响应。 ... [详细]
  • NFS(Network File System)即网络文件系统,是一种分布式文件系统协议,主要用于Unix和类Unix系统之间的文件共享。本文详细介绍NFS的配置文件/etc/exports和相关服务配置,帮助读者理解如何在Linux环境中配置NFS客户端。 ... [详细]
  • 深入探讨ASP.NET中的OAuth、JWT与OpenID Connect
    本文作为前文关于OAuth2.0和使用.NET实现OAuth身份验证的补充,详细阐述了OAuth与JWT及OpenID Connect之间的关系和差异,旨在提供更全面的理解。 ... [详细]
  • Active Object设计模式解析
    Active Object设计模式通过引入代理(Proxy)和命令(Command)模式,实现了方法调用与执行的解耦,从而支持异步操作和提高系统的并发处理能力。本文将深入探讨Active Object模式的工作原理及其应用场景。 ... [详细]
  • Consul 单节点与集群环境构建指南
    本文详细介绍了如何安装和配置 Consul 以支持服务注册与发现、健康检查等功能,包括单节点和集群环境的搭建步骤。 ... [详细]
  • 本文探讨了在Cloudera Distribution Including Apache Hadoop (CDH) 中添加位于新网络段的新节点后,如何解决Impala查询失败的问题。通过分析错误日志和网络配置,提供了详细的解决方案。 ... [详细]
  • 本文介绍了如何在三台CentOS 7.5虚拟机上通过Docker部署RabbitMQ集群,包括环境准备、容器创建、集群配置及故障处理等内容。 ... [详细]
  • Python作为一种广泛使用的高级编程语言,以其简洁的语法、强大的功能和丰富的库支持著称。本文将详细介绍Python的主要特点及其在现代软件开发中的应用。 ... [详细]
  • 如何有效提高Windows 7的启动性能
    本文探讨了提高Windows 7启动速度的方法,包括微软官方建议及用户可采取的具体措施,旨在帮助用户解决启动缓慢的问题。 ... [详细]
  • 深入解析 RocketMQ 的架构与应用
    本文详细介绍了 RocketMQ 的核心特性、系统架构、部署模式以及如何编写生产者和消费者的代码,通过具体案例探讨了其在实际项目中的应用。 ... [详细]
  • 本文详细介绍了如何设置局域网,并确保网络中的所有计算机能够相互访问和共享安装的软件。包括物理连接检查、TCP/IP设置、网络协议配置等多个方面。 ... [详细]
  • Kodi(原名XBMC)是一款由XBMC基金会开发的免费开源媒体播放器应用,支持多种操作系统和硬件平台。该软件以其强大的多媒体处理能力和用户友好的界面受到广泛欢迎。 ... [详细]
author-avatar
心在流血谁懂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有