Standard Library Extension
In version 6.1
, Swoole-Cli
will introduce extended syntax for the PHP
standard library, providing additional new syntax while maintaining full compatibility with the official PHP
syntax. These new extended syntax features are entirely optional and do not affect existing PHP
code. The standard library extension syntax will be implemented in the stdext
module of ext-swoole
.
swoole-cli
users can use it directly without any additional parameters.ext-swoole
users need to add--enable-swoole-stdext
during compilation to enable it.stdext
can be used inphp-cli
,php-fpm
,cli-http-server
, andswoole
modes.
1. Object-Oriented Basic Types
Version 6.1
implements object-oriented encapsulation of basic types such as array
, string
, and stream
, supporting method calls in an object-oriented style. Built-in methods can be directly called on variables of these types.
The built-in methods for basic types still utilize the PHP
standard library. These methods correspond one-to-one with PHP
's str_
or array_
series functions. For example, $text->replace()
corresponds to the str_replace
function. The underlying implementation only adjusts function names, slightly modifies the parameter order for a few methods, and adjusts parameter and return value types for several others. For details, please refer to the Swoole Documentation.
$text = 'Hello Swoole';
var_dump($text->length());
echo $text->lower()->replace("swoole", "php");
2. Typed-Array
Typed arrays can enforce type constraints, such as Map
or ArrayList
, and restrict the types of keys and values. Multi-level nested formats are supported. Typed arrays are still essentially array
types, with parameter checks performed only during write operations. Typed arrays can be passed as array
to other functions or class methods.
$array = typed_array('<int, string>');
$array[1000] = "hello"; // Correct
$array[2000] = 2025; // Error, throws an exception
✨ New Features
- Default llHTTP Parser: Starting from v6.1.0, Swoole uses the higher-performance
llhttp
as the default HTTP message parser, replacinghttp_parser
. - Coroutine-Based File Download:
Swoole\Coroutine\Http\Client
now supports full coroutine-based file download operations. - Asynchronous File Truncation: Added support for
iouring ftruncate
, expanding asynchronous file operation capabilities.
🐛 Bug Fixes
- Thread Mode Optimization:
- Fixed the issue where
hook_flags
settings were ineffective inSWOOLE_THREAD
mode. - Fixed the issue where heartbeat threads held invalid pointers and accessed freed memory after worker threads restarted due to reaching the
max_request
limit. - Fixed the issue where
Task
processes could not be restarted individually.
- Fixed the issue where
- Stability Improvements:
- Fixed the issue where synchronous processes (including manager and task processes) could not use
process::signal()
properly. - Corrected the resource cleanup mechanism when worker processes exit abnormally.
- Resolved compilation failures on the Cygwin platform.
- Fixed the issue where curl could not reuse sockets when keepalive was enabled.
- Improved the logging component to address data consistency issues in multi-threaded mode.
- Fixed compilation failures of the
futex
feature inio_uring
onubuntu24.04
. - Fixed compilation failures caused by struct sequential assignment on older Linux systems.
- Fixed the issue where the
id
property of directly instantiatedSwoole\Process
objects was uninitialized. - Fixed the missing
--enable-zstd
option in composer.json when compiling Swoole withPIE
.
- Fixed the issue where synchronous processes (including manager and task processes) could not use
- Compatibility Improvements:
- Fixed compatibility issues with
php_swoole_register_shutdown_function
on PHP 8.5. - Corrected the handling of null parameters in
Swoole\Table::get()
.
- Fixed compatibility issues with
- Protocol Handling:
- Optimized the handling of duplicate fields in HTTP request and response headers to ensure proper merging.
- Fixed the issue where a warning about an unknown compression method was triggered when processing client
Accept-Encoding: zlib
headers withoutzlib
installed.
Deprecations
- Support for the
select
event mechanism has been removed.select
only supports event monitoring for up to1024
handles and has significant limitations. On platforms that do not supportepoll/kqueue
,poll
will be used as the event polling mechanism, enabling support for high concurrency even on theCygwin
platform. - The second parameter
$waitEvent
inSwoole\Server::stop()
has been deprecated. Please use thereload_async
parameter instead.
🛠️ Architectural Optimizations
Version 6.1
adds extensive unit tests for core modules, increasing test coverage to 86%. By systematically supplementing test cases and refactoring underlying code logic, combined with full static code analysis using the clang-tidy
toolchain, automated code formatting and redundancy cleanup have been achieved, significantly improving code readability and maintainability.
- Optimized the underlying
SSL
module. - Optimized the underlying
Socket
module. - Refactored the underlying synchronous signal module.
- Refactored the underlying
reactor
andstring
modules. - Refactored the underlying dynamic log file reloading mechanism based on
SIGRTMIN
signals. - Optimized the underlying static file server by removing C-style code and unifying it into C++-style code.
- Optimized thread initialization logic to improve multi-threaded performance. Memory for independent management structures is now managed separately, eliminating the need for locks during thread creation and exit.
- Removed the
socket_dontwait
option from the underlyingasync
module, as it is deprecated. - Improved test coverage for
WebSocket
mask handling. - Optimized the
sendfile
functionality to avoid setting thetcp_cork
option for files smaller than64KB
. - Added unit tests for
aarch64
andmacOS
environments. - Optimized underlying client network address resolution and settings.
- Added
ssl_cafile/ssl_capath
configuration items for theServer
module. - Added the
print_backtrace_on_error
configuration, which prints C function stack traces when location errors occur. - Added the
Address Sanitizer
debugging tool. - Added
Cygwin
testing, significantly improving compatibility and stability on theCygwin
platform. - On
macOS
systems, thekqueue
event mechanism does not support cross-process pipe event monitoring, making it impossible to useSWOOLE_PROCESS
mode andTask
processes. Therefore, the system defaults to usingpoll
as the underlying event mechanism. To enablekqueue
, manually activate it usingSwoole\Server::set(['enable_kqueue' => true])
andswoole_async_set(['enable_kqueue' => true])
.
⚠️ Note
- This version is an
RC
(Release Candidate) and not an official release. It is recommended for use only in testing environments and should not be used in production.
🙏 Thank You
Sincere thanks to @matyhtf @jingjingxyk @sy-records @KIMDONGYEON00 @ServBayDev @NathanFreeman and all contributors for their professional efforts. Swoole is stronger because of you! Wishing all open-source community partners good health, success in all endeavors, and smooth work.
标准库扩展
6.1
版本 Swoole-Cli
将增加 PHP
标准库扩展语法,在保持对 PHP
官方语法完整兼容的同时,提供一些额外的新语法。这些新的扩展语法均是可选的,不影响现有的PHP
代码。标准库扩展语法将在ext-swoole
的stdext
模块中实现。
swoole-cli
用户,可直接使用,无需设置任何额外的参数ext-swoole
的用户,需在编译时添加--enable-swoole-stdext
来启用stdext
在php-cli
、php-fpm
、cli-http-server
、swoole
模式均可使用
1. 基础类型对象化
6.1
版本实现了array
/string
/stream
基础类型的对象化封装,支持面向对象风格的方法调用,可直接对这些类型的变量调用内置方法。
基础类型的内置方法实际上仍然是调用PHP
标准库实现的,内置方法与PHP
的str_
或array_
系列函数是一一对应关系,例如$text->replace()
对应的函数是str_replace
。底层仅调整了函数名称,少量方法调整了参数顺序,还有几个方法调整了参数和返回值类型,详情请查看 Swoole 文档。
$text = 'Hello Swoole';
var_dump($text->length());
echo $text->lower()->replace("swoole", "php");
2. Typed-Array 强类型数组
可限制数组类型,如:Map
或ArrayList
,以及限制key
与value
的类型。支持多层嵌套格式。typed array
实际上依然是array
类型,底层仅在写入时进行参数检查。typed array
可作为array
传递给其他函数或类方法。
$array = typed_array('<int, string>');
$array[1000] = "hello"; // 正确
$array[2000] = 2025; // 错误,抛出异常
✨ 新特性
- llHTTP 默认解析器:自 v6.1.0 起,Swoole 采用性能更优的
llhttp
替代http_parser
作为默认 HTTP 报文解析器。 - 协程化文件下载:
Swoole\Coroutine\Http\Client
现支持完整的协程化文件下载操作。 - 异步文件截断:新增
iouring ftruncate
支持,扩展异步文件操作能力。
🐛 Bug 修复
- 线程模式优化:
- 修复
SWOOLE_THREAD
模式下hook_flags
设置失效问题。 - 修复因工作线程达到
max_request
限制重启后,导致心跳线程持有失效指针并访问已释放内存的问题。 - 修复不支持单独重启
Task
进程的问题。
- 修复
- 稳定性提升:
- 修复同步进程(含 manager 进程和 task 进程)无法正常使用
process::signal()
的问题。 - 修正工作进程异常退出时的资源清理机制。
- 解决 Cygwin 平台编译失败问题。
- 修复curl开启keepalive无法复用socket的问题。
- 改进日志组件以解决多线程模式下的数据一致性问题。
- 修复
io_uring
的futex
特性在ubuntu24.04
编译失败的问题。 - 修复结构体顺序赋值在低版本的linux系统会编译失败的问题。
- 修复直接实例化的
Swoole\Process
对象其id
属性未初始化的问题。
- 修复同步进程(含 manager 进程和 task 进程)无法正常使用
- 修复使用
PIE
编译swoole时,composer.json文件缺少--enable-zstd
的选项的问题。 - 兼容性改进:
- 修复 PHP 8.5 下
php_swoole_register_shutdown_function
兼容性问题。 - 修正
Swoole\Table::get()
对 null 参数的处理。
- 修复 PHP 8.5 下
- 协议处理:
- 优化
HTTP
请求头与响应头中重复字段的处理机制,确保其正确合并。 - 修复当系统没有
zlib
时,处理客户端Accept-Encoding: zlib
请求头会触发未知压缩方法警告的问题。
- 优化
废弃
- 不再支持
select
事件机制,select
仅支持1024
个句柄的事件监听,存在严重缺陷,在不支持epoll/kqueue
的平台将使用poll
作为事件轮训机制,这使得Cygwin
平台下也可以支持大量并发。 - 在
Swoole\Server::stop()
方法中,第二个参数$waitEvent
已被废弃,请使用reload_async
参数。
🛠️ 架构优化
6.1
版本增加了大量核心模块的单元测试,测试覆盖率提升至86%。通过系统性地补充测试用例并重构底层代码逻辑,同时结合clang-tidy
工具链开展全量静态代码分析,实现了代码的自动化格式规整与冗余清理,使得代码可读性与可维护性得到显著提升。
- 优化底层
SSL
模块。 - 优化底层
Socket
模块。 - 重构底层同步信号模块。
- 重构底层
reactor
和string
模块。 - 重构底层基于
SIGRTMIN
信号实现日志文件动态重载。 - 优化底层静态文件服务器,移除C风格代码,统一为C++风格的代码。
- 优化线程初始化逻辑,提高多线程性能,独立管理结构内存,线程创建和退出不需要再加锁。
- 移除底层
async
模块中的socket_dontwait
选项,该选项已废弃。 - 完善
WebSocket
掩码处理的测试覆盖。 - 优化
sendfile
功能,当文件小于64KB
时不设置tcp_cork
选项。 - 增加
aarch64
和macOS
环境下的单元测试。 - 优化底层客户端网络地址解析和设置。
- 为
Server
模块增加ssl_cafile/ssl_capath
配置项。 - 增加
print_backtrace_on_error
配置,此配置可以在发生位置错误时候,打印C函数的堆栈。 - 增加
Address Sanitizer
调试工具。 - 增加
Cygwin
测试,显著提升Cygwin
平台下的兼容性和稳定性 - 在
macOS
系统中,由于kqueue
事件机制不支持跨进程管道事件监听,导致无法使用SWOOLE_PROCESS
模式和Task
进程功能。因此,系统将默认采用poll
作为底层事件机制。如需启用kqueue
,可以通过Swoole\Server::set(['enable_kqueue' => true])
和swoole_async_set(['enable_kqueue' => true])
方法手动开启。
⚠️ 注意
- 该版本为
RC
版本非正式版,仅推荐在测试环境中使用,不建议在生产环境中使用。
🙏 致谢
诚挚感谢 @matyhtf @jingjingxyk @sy-records @KIMDONGYEON00 @ServBayDev @NathanFreeman 所有贡献者的专业付出,Swoole 因你们而更强大!愿开源社区伙伴身体健康,万事如意,工作顺利。
翻译成英文