Expand description
SZ-Rust Runtime — Swoole/Worker 适配主入口
§PHP 对齐
对齐 PHP topthink/think-swoole 的运行时模型:
| PHP Swoole | Rust SZ-Rust | 说明 |
|---|---|---|
Swoole\Runtime::enableCoroutine() | SzRuntime::block_on(fut) | 协程入口 |
swoole_cpu_num() | num_cpus::get() | 默认 worker 数 |
worker_num 配置项 | SzRuntime::with_worker_threads(n) | 自定义 worker 数 |
Swoole\Process::signal() | tokio::signal + CancellationToken | 信号处理 |
Swoole\Timer::tick() | tokio::time::interval + tokio::select! | 定时任务 |
swoole_event::defer() | SzRuntime::spawn(fut) | 异步任务 |
§模块结构
| 模块 | 对齐 PHP | 子任务 |
|---|---|---|
runtime::worker | worker_num 配置 | 9.2 |
runtime::spawn | swoole_event::defer | 9.3 |
runtime::queue | think-queue 消费者 | 9.4 |
runtime::mqtt | 长连接 | 9.5 |
runtime::websocket | think-worker | 9.6 |
runtime::scheduler | Crontab | 9.7 |
runtime::shutdown | 优雅关闭 | 9.8 |
runtime::signal | SIGTERM/SIGINT | 9.9 |
§关键决策
- 不修改
sz-orm-scheduler内部 API:保持CronScheduler::start()/stop()向后兼容, 在 sz-rust 侧用tokio::time::interval+try_fire_due()重写循环。 - 使用
CancellationToken统一关闭广播:替代AtomicBool+oneshot,支持父子层级。 - 双平台信号处理:Unix 用
SignalKind::terminate/interrupt,Windows 用ctrl_c/ctrl_close。
Re-exports§
pub use mqtt::MqttRuntime;pub use mqtt::MqttRuntimeConfig;pub use queue::QueueConsumer;pub use queue::QueueRuntime;pub use queue::QueueRuntimeConfig;pub use scheduler::SchedulerRuntime;pub use shutdown::GracefulShutdown;pub use signal::shutdown_signal;pub use spawn::spawn_with_token;pub use websocket::WebSocketRuntime;pub use websocket::WebSocketRuntimeConfig;pub use worker::WorkerConfig;
Modules§
- mqtt
- sz-orm-mqtt 长连接接入
- queue
- sz-orm-queue 消费者接入
- scheduler
- 定时任务调度接入
- shutdown
- 优雅关闭协调器
- signal
- 信号处理
- spawn
- tokio::spawn 异步任务
- websocket
- sz-orm-websocket 服务端接入
- worker
- Worker 数量配置
Structs§
- SzRuntime
- SZ-Rust 异步运行时