Skip to content

v6.2 的新增特性 #99

@matyhtf

Description

@matyhtf

初步设想,最终版本可能会有所调整。

1. 增加 Co::setTimeLimit() 函数

function Co::setTimeLimit(double $timeout);

为协程设置一个最大运行时间,超时后会触发 Swoole\Coroutine\TimeoutExeception 异常。

function onRequest($req, $resp) {
try {
    // 设置当前协程最大执行时间为 2 秒
    Co::setTimeLimit(2.0);
    // 执行业务逻辑 ...
    $resp->status(200);
    $resp->end($output);
} catch(Swoole\Coroutine\TimeoutExeception $e) {
    $resp->status(504);
    $resp->end("REQUEST TIMEOUT");
} catch(Swoole\Coroutine\CanceledExeception $e) {

}

2. 增加新的协程客户端

  • ext-pdo_firebird
  • ext-ftp
  • ext-ssh2
  • system()
  • passthru()

3. 增强 PHP Stream 异步支持,允许独立使用

允许在关闭 Stream HOOK 后,使用 async. 前缀单独开启异步PHP Stream支持。

// 关闭 Runtime Hook
Swoole\Runtime::setHookFlags(0);

$file = fopen("async.file:///data/your_file.log", "a+");
// tcp
$tcp = stream_socket_client("async.tcp://127.0.0.1:9501"); // IPv4
$tcp = stream_socket_client("async.tcp://[::1]:9501"); // IPv6
// udp
$tcp = stream_socket_client("async.udp://127.0.0.1:9502");
// ssl
$ssl = stream_socket_client("async.ssl://127.0.0.1:9501");
// unix
$ssl = stream_socket_client("async.udg:///var/run/docker.sock"); // unix dgram
$ssl = stream_socket_client("async.unix:///var/run/docker.sock"); // unix stream

Runtime Hook 不同,async. 协议的 stream 总是可用的。

4. 支持 PHP 8.5,废弃 PHP 8.1

根据 https://www.php.net/supported-versions.php ,8.1 版本将在 31 Dec 2025 终止维护,进入End of Life 阶段。因此 swoole 也将不再支持此版本。

5. 绑定检测打印堆栈

Socket 绑定检测的 fatal error 打印协程堆栈

  • 读写操作 socket 失败的协程堆栈
  • 当前占有 socket 的协程堆栈

6. 废弃所有同步阻塞特性的支持

6.2版本之后,将不再包含任何同步 IO 的支持

  • Swoole\Client
  • swoole_client_select()
  • Server::taskWaitMulti() 同步模式
  • Server MsgQueue 支持
  • Swoole\Process 同步模式

7. SAPI 调整,仅支持 Cli

移除其他SAPI的支持,仅限于cliphpdbg 命令行模式

8. 新增 URL Rewrite 模块

仅用于静态文件处理器,可改写文件路径。示例:

$http->set([
    'document_root' => __DIR__,
    'enable_static_handler' => true,
    'static_handler_locations' => ['/static'],
    'url_rewrite_rules' => [
        '~^/view/post/(\d+)$~' => '/static/$1.html',
        '/article/' => '/static/article/'
    ]
]);

支持正则匹配和字符串匹配两种方式:

  1. 正则:必须以~作为开头和结尾
  2. 字符串

作用是将匹配到的 URL 替换为实际的 URL 并转发给静态处理器。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions