Linux Socket相关预先设置

高并发socket  服务优化

1.  1客户端部分  

    • net.ipv4.ip_local_port_range = 1500  61000
    • net.ipv4.tcp_fin_timeout = 20 
          net.ipv4.tcp_tw_recycle = 1
         net.ipv4.tcp_tw_reuse = 1 
1.2. 服务端部分

  • net.core.somaxconn = 1024
  •  txqueuelen = 5000
  • net.core.netdev_max_backlog = 1024
  • net.ipv4.tcp_max_syn_backlog = 1024


2     修改/etc/security/limits.conf文件,在文件中添加如下行:
  bruce soft nofile 10240
bruce  hard nofile 10240
       可用'*'号表示修改所有用户的限制;
      soft或hard指定要修改软限制还是硬限制;10240则指定了想要修改的新的限制值,即最大打开文件数(请注意软限制值要小于或等于硬限制)。修改完后保存文件。


3   修改此硬限制的方法是修改/etc/rc.local脚本,在脚本中添加如下行:
  echo 22158 > /proc/sys/fs/file-max
  这是让Linux在启动完成后强行将系统级打开文件数硬限制设置为22158.修改完后保存此文件。
  完成上述步骤后重启系统,一般情况下就可以将Linux系统对指定用户的单一进程允许同时打开的最大文件数限制设为指定的数值。


4   内核参数sysctl.conf的优化

/etc/sysctl.conf 是用来控制linux网络的配置文件,对于依赖网络的程序(如web服务器和cache服务器)非常重要,RHEL默认提供的最好调整。
推荐配置(把原/etc/sysctl.conf内容清掉,把下面内容复制进去):
  net.ipv4.ip_local_port_range = 1024 65535
  net.core.rmem_max=16777216
  net.core.wmem_max=16777216
  net.ipv4.tcp_rmem=4096 87380 16777216
  net.ipv4.tcp_wmem=4096 65536 16777216
  net.ipv4.tcp_fin_timeout = 10
  net.ipv4.tcp_tw_recycle = 1
  net.ipv4.tcp_timestamps = 0
  net.ipv4.tcp_window_scaling = 0
  net.ipv4.tcp_sack = 0
  net.core.netdev_max_backlog = 30000
  net.ipv4.tcp_no_metrics_save=1
  net.core.somaxconn = 262144
  net.ipv4.tcp_syncookies = 0
  net.ipv4.tcp_max_orphans = 262144
  net.ipv4.tcp_max_syn_backlog = 262144
  net.ipv4.tcp_synack_retries = 2
  net.ipv4.tcp_syn_retries = 2

5   经典步骤

Assumptions
This howto assumes that the machine being tuned is involved in supporting high-bandwidth applications. Making these modifications on a machine that supports multiple users and/or multiple connections is not recommended - it may cause the machine to deny connections because of a lack of memory allocation.
The Steps
Make sure that you have root privleges.

Type: sysctl -p | grep mem
This will display your current buffer settings. Save These! You may want to roll-back these changes

Type: sysctl -w net.core.rmem_max=8388608 
This sets the max OS receive buffer size for all types of connections.

Type: sysctl -w net.core.wmem_max=8388608 
This sets the max OS send buffer size for all types of connections.

Type: sysctl -w net.core.rmem_default=65536 
This sets the default OS receive buffer size for all types of connections.

Type: sysctl -w net.core.wmem_default=65536 
This sets the default OS send buffer size for all types of connections.

Type: sysctl -w net.ipv4.tcp_mem='8388608 8388608 8388608' 
TCP Autotuning setting. "The tcp_mem variable defines how the TCP stack should behave when it comes to memory usage. ... The first value specified in the tcp_mem variable tells the kernel the low threshold. Below this point, the TCP stack do not bother at all about putting any pressure on the memory usage by different TCP sockets. ... The second value tells the kernel at which point to start pressuring memory usage down. ... The final value tells the kernel how many memory pages it may use maximally. If this value is reached, TCP streams and packets start getting dropped until we reach a lower memory usage again. This value includes all TCP sockets currently in use." 

Type: sysctl -w net.ipv4.tcp_rmem='4096 87380 8388608' 
TCP Autotuning setting. "The first value tells the kernel the minimum receive buffer for each TCP connection, and this buffer is always allocated to a TCP socket, even under high pressure on the system. ... The second value specified tells the kernel the default receive buffer allocated for each TCP socket. This value overrides the /proc/sys/net/core/rmem_default value used by other protocols. ... The third and last value specified in this variable specifies the maximum receive buffer that can be allocated for a TCP socket." 

Type: sysctl -w net.ipv4.tcp_wmem='4096 65536 8388608' 
TCP Autotuning setting. "This variable takes 3 different values which holds information on how much TCP sendbuffer memory space each TCP socket has to use. Every TCP socket has this much buffer space to use before the buffer is filled up. Each of the three values are used under different conditions. ... The first value in this variable tells the minimum TCP send buffer space available for a single TCP socket. ... The second value in the variable tells us the default buffer space allowed for a single TCP socket to use. ... The third value tells the kernel the maximum TCP send buffer space." 

Type:sysctl -w net.ipv4.route.flush=1
This will enusre that immediatly subsequent connections use these values. 

Quick Step
Cut and paste the following into a linux shell with root privleges:

sysctl -w net.core.rmem_max=8388608
sysctl -w net.core.wmem_max=8388608
sysctl -w net.core.rmem_default=65536
sysctl -w net.core.wmem_default=65536
sysctl -w net.ipv4.tcp_rmem='4096 87380 8388608'
sysctl -w net.ipv4.tcp_wmem='4096 65536 8388608'
sysctl -w net.ipv4.tcp_mem='8388608 8388608 8388608'
sysctl -w net.ipv4.route.flush=1


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值