性能调优ulimit增加TCP连接数最大限制

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011644231/article/details/88880229

TCP连接数限制

Linux系统中tcp连接数是有最大限制的,即是进程可打开的最大文件描述个数,以下命令查看

ulimit -n

高负载的服务器通过修改ulimit参数达到合理规划用户使用资源和系统资源的目的。

用户级别

修改最大连接数

  1. 修改系统参数实现

ulimit -SHn 65535

-H硬限制是实际的限制,-S软限制是warnning限制,只会做出warning.
如果运行ulimit命令修改的时候没有加上SH,就是两个参数一起改变.

  1. 修改pam模块配置实现
  • session required /lib/security/pam_limits.so
  • 修改/etc/security/limits.conf如下举例

* soft nofile 65536
* hard nofile 65536
*代表所有用户,当然也可以指定用户如root

确认修改是否生效

项目中遇到修改后虽然命令查看已经是修改后的值,但是进程连接的tcp个数还是系统默认的1024导致接入拒绝

查看进程实际的最大连接数

cat /proc/进程pid/limits
Max open files 就是当前进程的实际值

系统级别

查看总的系统打开文件限制

cat /proc/sys/fs/file-max

若要修改可以在rc.local加

echo 你要的 > /proc/sys/fs/file-max

展开阅读全文

请教高手TCP连接数限制

08-12

我发现在同时连接100个左右以后的连接就连不上了。究竟是那方面的问题呢?rnrn测试代码:3个文件rnrnrn=============================rnUnit3.pasrn============================rnunit Unit3;rnrninterfacernrnusesrn Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,rn Dialogs, WSOcket, ComCtrls, StdCtrls;rnrntypern TForm3 = class(TForm)rn ListView1: TListView;rn Button1: TButton;rn procedure FormCreate(Sender: TObject);rn procedure Button1Click(Sender: TObject);rn privatern Private declarations rn procedure COnnected(Sender: TObject; error: Word);rn publicrn Public declarations rn end;rnrnvarrn Form3: TForm3;rnrnimplementationrnrn$R *.dfmrnrnvarrn Clients: array of TWSocket;rnrnprocedure TForm3.COnnected(Sender: TObject; error: Word);rnvarrn li: tlistitem;rnbeginrn li := listView1.FindData(0, Sender, True, True);rn if assigned(Li) thenrn beginrn if error <> 0 thenrn beginrn li.SubItems[0] := 'fail:' + Inttostr(error);rn endrn elsern li.subitems[0] := 'ok:';rn end;rn rnend;rnrnprocedure TForm3.Button1Click(Sender: TObject);rnvarrn I: Integer;rn W: twsocket;rnbeginrn for I := 0 to listview1.items.count - 1 dorn beginrn Clients[I].Addr := listView1.Items[I].Caption;rn Clients[I].Port := '7000';rn Clients[I].OnSessionConnected := Connected;rn Clients[i].Connect;rnrn end;rnend;rnrnprocedure TForm3.FormCreate(Sender: TObject);rnvarrn Strings: TStrings;rn I: Integer;rn li: tlistitem;rnbeginrn strings := tstringlist.Create;rn strings.LoadFromFile(GetCurrentDir + '\ip.txt');rn setlength(clients, strings.count);rn for I := 0 to strings.Count - 1 dorn beginrnrn Clients[I] := TWSocket.Create(nil);rn li := listView1.Items.Add;rn li.Caption := strings[i];rn li.Data := clients[I];rn li.SubItems.Add('');rn end;rn strings.Free;rnend;rnrnend.rnrnrn======================================rnUnit3.dfmrn======================================rnobject Form3: TForm3rn Left = 0rn Top = 0rn Caption = 'Form3'rn ClientHeight = 238rn ClientWidth = 337rn Color = clBtnFacern Font.Charset = DEFAULT_CHARSETrn Font.Color = clWindowTextrn Font.Height = -11rn Font.Name = 'Tahoma'rn Font.Style = []rn OldCreateOrder = Falsern OnCreate = FormCreatern PixelsPerInch = 96rn TextHeight = 13rn object ListView1: TListViewrn Left = 0rn Top = 0rn Width = 337rn Height = 238rn Align = alClientrn Columns = rn TabOrder = 0rn ViewStyle = vsReportrn endrn object Button1: TButtonrn Left = 200rn Top = 72rn Width = 75rn Height = 25rn Caption = 'Button1'rn TabOrder = 1rn OnClick = Button1Clickrn endrnendrn 论坛

没有更多推荐了,返回首页