来自:桶哥的一篇关于swoole的心跳的文章,作为Swoole顾问(顾得上就问,是为「顾问」)得推一下这篇文章,最后只留下一配置,其实我也不是太明白原理,我在想如果是局域网里还需要心跳?
——————————————————————————————————————————————————————————————————————
swoole提供了一个心跳的功能,很多朋友感到困惑。
心跳是什么?
顾名思义,心跳是判断一个事物生还是死的一个标准,在swoole里,心跳是指用来判断一个连接是正常还是断开的。
从TCP协议说起
我们都知道一个五元组标识一个网络连接,创建一个连接有三次握手,而断开一个连接有四次挥手。不管是服务器还是客户端
发起连接的关闭,都会完整的走完四次挥手的过程,这样,一切很完美,系统回收这个fd,应用层也可以通过onClose回调处理相关的事情.
fd是什么?
fd学名是文件描述符,在unix的哲学就是一切皆文件中,这个fd就是系统层暴露给业务层的用来表示一个五元组网络连接的标识。你可以简单的理解为一个索引,通过对这个fd的操作,系统层可以找到相应的连接而且进行的一系列操作,如发送数据到网瞳,进行连接关闭等等。
为什么要心跳?
刚才提到,如果我们要关闭某个连接,我们可以在业务层对fd发起关闭连接的操作,以swoole为例:
$server->close($fd);
正常情况下,都会走完整个四次挥手,(swoole会有onClose回调),系统回收fd,以待分配给其他的连接。
那系统为什么要回收fd,因为fd资源是有限的,所以必需重复利用。
但在某些情况下,如突然拔掉网线或蓝翔演习挖断光缆,服务端并不能感知到这个连接的异常,但实际上是这个连接已经失效了,如果没有一个回收机制,这类连接将用光所有的fd,导致系统不再能接受新的连接请求,所以就有了心跳机制。
什么是心跳机制?
心跳机制就是业务层来提供一个连接是否存活的一个方法,让系统能判定一个连接是否失效。一般有两种实现方式:
1: 客户端定时发送一个心跳包,告诉服务器我还活着,服务器定时检测所有客户端列表,看他们最后一个心跳包的时间是否过长,如果过长,则认为已无心跳,判定为死连接,主动关闭这个连接。
2: 服务器定时询问所有的客户端,你们还活着么?如果活着,给我个回馈,没得到回馈的客户端,格杀勿论。
两种心跳方案有什么区别?
第一种方案,对服务器和网络的压力更小,而且更具有灵活性,但需要客户端配合定时发送心跳包。
第二种方案,对服务器和网络压力更大,不建议使用。
心跳在swoole里的实现
swoole采用的是第一种方案
swoole会在主进程独立起一个心跳线程,通过定时轮询所有的连接,来判断连接的生死,所以swoole的心跳不会堵塞任何业务逻辑。
那怎么判断连接的生死了?swoole在connection结构体中有 time_t last_time 字段,用来存放最后一次收包的时间戳,进而通过与这个时间对比来判定是否存活
于是,swoole有两个配置:
heartbeat_check_interval: 服务器定时检测在线列表的时间
heartbeat_idle_time: 连接最大的空闲时间 (如果最后一个心跳包的时间与当前时间之差超过这个值,则认为该连接失效)
配置建议
建议 heartbeat_idle_time 为 heartbeat_check_interval 的两倍多一点。
这个两倍是为了进行容错,允许丢一个包
而多一点是考虑到网络的延时。
你可以跟据实际的业务来调整这个容错率(允许丢几个包)。
补充
1、系统层面也提供心跳机制,只不过粒度相对比较粗,而且时间稍长,没有应用层灵活
2、swoole还提供ping的功能,通过配置ping值,swoole内核可以判断只是一个心跳包,而不会,也没必要把数据包转发应用层(onReceive)。
3、心跳不只是swoole独有,大多数tcp的网络服务都会考虑到这个问题

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software