当前位置:网站首页>Introduction to long connection
Introduction to long connection
2022-06-11 13:16:00 【I will execute】
A long connection
A long connection 、 Short connection 、 heartbeat
HTTP Deal with the TCP/IP Relationship of agreement :
- HTTP The long connection and the short connection of are essentially TCP Long and short connections .HTTP Belongs to the application layer protocol , Use... In the transport layer TCP agreement , Use... In the network layer IP agreement . IP The protocol mainly solves the problem of network routing and addressing ,TCP The agreement mainly deals with how to IP Deliver packets reliably above the layer , Make the receiving end on the network receive all the packets sent by the sender , And the order is the same as the sending order .TCP The agreement is reliable 、 Connection oriented .
How to understand HTTP Protocol is stateless :
- HTTP Protocol is stateless , It means that the protocol has no memory for transaction processing , The server does not know what the client status is . in other words , There is no connection between opening a web page on a server and the last web page opened on this server .HTTP Is a stateless connection oriented protocol , Stateless does not mean HTTP Can not keep TCP Connect , Not to mention HTTP It uses UDP agreement ( There is no connection ).
What is long connection 、 Short connection ?
- stay HTTP/1.0 Short connection is used by default in . in other words , Client and server every time HTTP operation , Just one connection , Disconnect at end of task . When a client browser accesses HTML Or other types Web The page contains other Web resources ( Such as JavaScript file 、 image file 、CSS Documents, etc. ), Every time I meet such a Web resources , The browser will recreate a HTTP conversation .
And from HTTP/1.1 rise , Use long connection by default , To maintain connection characteristics . Using long connected HTTP agreement , This line will be added to the response header :
Connection:keep-alive
With long connections , When a web page is opened , Transport between client and server HTTP Data TCP Connection will not close , When the client accesses this server again , Will continue to use this established connection .Keep-Alive Not permanently connected , It has a hold time , Different server software is available ( Such as Apache) Set this time in . To realize long connection, both client and server need to support long connection .
HTTP Long connection and short connection of protocol , Is essentially TCP Long connection and short connection of protocol .
TCP Connect :
- When network communication is used TCP When the agreement , Before the real read-write operation , A connection must be established between the client and the server , When the read-write operation is completed , Both parties can release the connection when they no longer need it . The establishment of the connection depends on “ Three handshakes ”, And release requires “ Four handshakes ”, Therefore, the establishment of each connection requires resource consumption and time consumption .
Schematic diagram of establishing connection with classic triple handshake :

The classic four times handshake closes the connection diagram :
Long connection short connection operation process :
The operation steps of short connection are :
- Establishing a connection —— The data transfer —— Close the connection … Establishing a connection —— The data transfer —— Close the connection
The procedure of long connection is :
- Establishing a connection —— The data transfer …( Keep connected )… The data transfer —— Close the connection
Short connection :
Concept
client And server Establish a connection with three handshakes ,client Send request message ,server Return response , One connection is complete .
At this time, both sides can initiate close operation , But it's usually client First initiate close operation . It can be seen from the above that , The short connection is usually only in client/server Pass a request operation between .
Advantages and disadvantages of short connection :
- It's easier to manage , The existing connections are all useful connections , There is no need for additional controls .
Use scenarios :
Usually, when the browser accesses the server, it is a short connection .
For the server , Long connections will consume the resources of the server , Moreover, users use the browser to access the server relatively infrequently
If there are hundreds of thousands , Millions of connections , The pressure on the server side will be very great , Even collapse .
So for a large amount of concurrency , Low request frequency , Short connections are recommended .
A long connection :
What is long connection ?
client towards server A connection ,server Accept client Connect , The two sides establish a connection .
Client And server After one read and write , The connection between them is not actively closed , Subsequent read and write operations will continue to use this connection .
Long connection life cycle :
Under normal circumstances , One TCP After a long connection is established , As long as the double does not make a shutdown request and there are no exceptions , This connection has always existed .
The operating system will not automatically shut it down , Even after the change of physical network topology, it can still be used .
So keep a connection for a few days 、 Several months 、 It's possible for a few years or more , As long as there are no exceptions or by the user ( application layer ) Active shut down .
The client and service order can always use this connection for data communication .
Advantages of long connections :
Long connection can save more TCP Set up and shut down operations , Reduce the impact of network congestion ,
When something goes wrong , You can prompt without closing the connection ,
Reduce CPU And memory usage , Because you don't need to establish and close connections often .
The disadvantages of long connections :
- When there are too many connections , Affect the performance and concurrency of the server .
Use scenarios :
The connection of database is to use TCP A long connection .
- RPC, Remote service invocation , On the server , One service process frequently calls another service process , Long connections can be used , Reduce connection time .
summary :
1. The use of long connection and short connection needs to be judged according to the application scenario
2. Long connections are not everything , It also needs maintenance .
Implementation of long connections
heartbeat
Most application layer protocols have HeartBeat Mechanism , Usually, the client sends a packet to the server every short period of time , Usually, the client sends a packet to the server every short period of time .
And transmit some data that may be necessary . A typical protocol for using heartbeat packets is IM, such as QQ/MSN/ Feixin and other agreements .
stay TCP In the mechanism of , There is a mechanism for heartbeat packets , That is to say TCP The option to :SO_KEEPALIVE.
The system is set by default 2 An hour's heart rate . But it can't check that the machine is powered off 、 Unplug the cable 、 Firewalls are broken .
And the logic layer may not be so easy to deal with disconnection . commonly , It's ok if it's just for living .
Why do I need a heartbeat mechanism ?
Because of the unreliability of the network , Are likely to TCP In the process of maintaining a long connection , Because of some emergency , For example, the network cable is pulled out , A sudden power failure, etc ,
The connection between the server and the client will be interrupted . In these emergencies , If it happens that there is no interaction between the server and the client , Then they can't find out in a short time that the other party has dropped the line .
Heartbeat mechanism can solve this kind of problem .
TCP Agreed KeepAlive Mechanism
Default KeepAlive The state is not open .
Need to put setsockopt take SOL_SOCKET.SO_KEEPALIVE Set to 1 It's opening KeepAlive state ,
And you can set three parameters :
tcp_keepalive_time ,tcp_keepalive_probes , tcp_keepalive_intvl,
respectively : How long does the connection go idle keepalive Of ack package 、 Send a few ack Only when the other party has broken the line when the package does not reply 、 Two ack The interval between packages .
A lot of network devices , In especial NAT Router , Because of the limitation of its hardware ( For example, memory 、CPU processing capacity ), Can't keep all the connections on it , So when necessary , Some inactive connections will be selected in the connection pool to kick off .
Typical practice is LRU, Connect the connection with no data for the longest time to T fall .
By using TCP Of KeepAlive Mechanism ( Modify that time Parameters ), You can make the connection generate some... Every other short period of time ack package , To reduce the risk of being kicked , Of course , The price is extra network and CPU burden .
How to realize heartbeat mechanism ?
Implement heartbeat mechanism :
Use TCP Agreement level keepalive Mechanism .
Implement custom heartbeat mechanism on application layer .
Although in TCP On the level of agreement , Provides keepalive The survival mechanism , But there are several disadvantages to using it :
It is not TCP Standard agreement for , And it's off by default .
TCP keepalive The mechanism depends on the implementation of the operating system , default keepalive The heartbeat time is Two hours , And right keepalive The system call is required for the modification of ( Or modify the system configuration ), Lack of flexibility .
TCP keepalive And TCP Protocol binding , So if you need to replace it with UDP When the agreement , keepalive The mechanism fails .
Use TCP Level of keepalive The mechanism saves traffic than the custom application layer heartbeat mechanism ,
边栏推荐
- Niu Mei and 01 Chuan
- kubernetes 证书合集
- .net core 抛异常对性能影响的求证之路
- tf. Data (II) -- parallelization tf data. Dataset generator
- [bug resolution] after uploading the image, cancel the upload and upload again. The last image still exists
- 想要实现在时序场景下“远超”通用数据库,需要做到哪几点?
- Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close its registration | geek headlines
- China's SaaS development lags behind that of the United States for 10 years, and it still needs to rely on innovation, open source, M & A and other strategies | archsummit
- Deep learning and CV tutorial (14) | image segmentation (FCN, segnet, u-net, pspnet, deeplab, refinenet)
- Condition debug of pycharm
猜你喜欢

求你了,不要再在对外接口中使用枚举类型了!

On the life extension of distributed locks -- redis based distributed locks

. 5 string

逆向学习入门-优秀的汇编调试工具OllyDbg

【后台交互】select 绑定后台传递的数据

live share使用体验
![InfoQ geek media's 15th anniversary essay solicitation - dynamic ranking of mobile terminal development [mui+flask+mongodb]](/img/92/50a92eb6b5c2ac3f58c499796a1e8c.png)
InfoQ geek media's 15th anniversary essay solicitation - dynamic ranking of mobile terminal development [mui+flask+mongodb]

On the continuing Life of Distributed Locks - - Distributed Locks Based on redis

常用字体介绍

Network information system emergency response
随机推荐
Network information system emergency response
qq内拉起支付宝h5支付功能
. 5 string
马斯克称自己不喜欢做CEO,更想做技术和设计;吴恩达的《机器学习》课程即将关闭注册|极客头条...
如何写出高性能代码(四)优化数据访问
UI inspiration analysis Notes 6: feature
Live share experience
如何学会花钱
Research on DB2 Database Reconstruction and table data migration
【问题总结】$t
關於分布式鎖的續命問題——基於Redis實現的分布式鎖
第五章 数据类型(四)
On the continuing Life of Distributed Locks - - Distributed Locks Based on redis
求你了,不要再在对外接口中使用枚举类型了!
Business practice of volcano engine cloud database VEDB in bytes
31w赛题奖金!当 AI for Science 撞上“先导杯”,会擦出什么样的火花?
About uni app configuration, app does not display the top title bar setting
In 2022, capture these 12 data and analyze trends!
应用编排Nomad与Kubernetes对比
【bug解决】上传图片后,取消这次上传 再次执行上传,上次的图片还存在