AMPHP v3 Skill
Version
This skill covers AMPHP v3 only — amphp/amp ^3, revolt/event-loop ^1, PHP 8.1+.
If you see v2 patterns (yield $promise, Amp\Loop::run(), Promise, Coroutine), treat them as wrong and rewrite using v3 equivalents. See docs/v2-v3.md for the full migration table.
Non-Negotiable Rules
These apply to every file you write or modify in an AMPHP project:
- - Always add
declare(strict_types=1) at the top of every PHP file. - Always pass
JSON_THROW_ON_ERROR to every json_encode / json_decode call. - Never use blocking I/O (
file_get_contents, PDO, curl_exec, sleep) inside the event loop — use async equivalents from amphp/file, amphp/mysql, amphp/http-client, Amp\delay(). - Always release mutex/semaphore locks in a
finally block — exceptions skip cleanup otherwise. - Always
buffer() or fully iterate HTTP response bodies — unread bodies block connection reuse.
For the full list of 19+ documented gotchas (buffer deadlocks, channel EOF, arrow function capture, Redis factory vs constructor, etc.), read docs/common-mistakes.md before writing async code.
Reference Files
Load only the file(s) relevant to the task. Do not load all files at once.
Docs
Writing
use imports — complete namespace paths for every class, function, and enum |
|
docs/packages.md | Starting a new project or adding a dependency —
composer require commands and package overview |
|
docs/common-mistakes.md | Before writing any async code — 19+ real bugs with wrong/correct examples |
|
docs/v2-v3.md | Migrating from AMPHP v2 or encountering
yield/
Promise/
Coroutine patterns |
Examples
TimeoutCancellation,
DeferredCancellation,
CompositeCancellation, propagating cancellation |
|
examples/sync.md |
LocalMutex,
LocalSemaphore,
LocalParcel,
Barrier,
LocalKeyedMutex,
RateLimitingSemaphore,
synchronized() |
|
examples/http-server.md | Minimal server, Router with route params, Middleware stack, TLS, Sessions, Static files, proxy setup |
|
examples/http-client.md | GET/POST, parallel requests,
ConnectionLimitingPool, interceptors, proxy, streaming response body |
|
examples/websocket.md | Echo server, push-only with drain pattern, broadcast gateway, WS client, streaming binary messages |
|
examples/byte-stream.md |
ReadableBuffer,
pipe(), GZIP compress/decompress, Base64 encode/decode,
splitLines() |
|
examples/pipelines.md |
Queue back-pressure,
Pipeline operators (
map,
filter,
tap),
concurrent(),
merge(),
concat() |
|
examples/parallel.md |
Task interface, fan-out with worker pool, IPC Channel progress reporting,
ChannelException handling |
|
examples/database.md | MySQL connection pool, transactions, prepared statements; Redis get/set/hash/pubsub/cache |
|
examples/file-io.md |
File\read/write/exists/getSize/openFile/listFiles/deleteDirectory/createDirectoryRecursively |
|
examples/cache.md |
LocalCache (LRU + TTL),
AtomicCache (compute-if-absent),
PrefixCache,
NullCache |
|
examples/interval.md |
Interval repeating timer,
enable/disable,
weakClosure() to prevent GC cycles,
EventLoop::delay/repeat/cancel |
|
examples/testing.md |
AsyncTestCase, constructing mock
Request objects,
League\Uri\Http::new(), phpunit CLI flags |
Workflows
CPU-bound workload split across multiple worker processes with IPC progress reporting |
|
workflows/tcp-server.md | Raw TCP server: echo, custom binary protocol, TLS mutual auth, graceful shutdown |
Templates
Copy-paste boilerplate for WebSocket handlers: echo, push-only, broadcast gateway |
|
templates/parallel-task.php | Copy-paste boilerplate for a worker
Task class with IPC progress + fan-out orchestration |
Scripts
php scripts/http-client-demo.php [url] — demo GET, parallel requests, ConnectionLimitingPool |
Resources
Legacy
One minimal usage example per key AMPHP class, organized by package |
AMPHP v3 技能
版本
本技能仅涵盖 AMPHP v3 — amphp/amp ^3、revolt/event-loop ^1、PHP 8.1+。
如果遇到 v2 模式(yield $promise、Amp\Loop::run()、Promise、Coroutine),请将其视为错误,并使用 v3 等效代码重写。完整迁移表请参见 docs/v2-v3.md。
不可妥协的规则
以下规则适用于您在 AMPHP 项目中编写或修改的每个文件:
- - 始终在每个 PHP 文件顶部添加 declare(stricttypes=1)。
- 始终在每个 jsonencode / jsondecode 调用中传递 JSONTHROWONERROR。
- 切勿在事件循环中使用阻塞 I/O(filegetcontents、PDO、curl_exec、sleep)——请使用 amphp/file、amphp/mysql、amphp/http-client、Amp\delay() 中的异步等效方法。
- 始终在 finally 块中释放互斥锁/信号量锁——否则异常会跳过清理。
- 始终对 HTTP 响应体执行 buffer() 或完全迭代——未读取的响应体会阻塞连接复用。
有关 19 个以上已记录的陷阱(缓冲区死锁、通道 EOF、箭头函数捕获、Redis 工厂与构造函数等)的完整列表,请在编写异步代码前阅读 docs/common-mistakes.md。
参考文件
仅加载与任务相关的文件。不要一次性加载所有文件。
文档
编写 use 导入时——每个类、函数和枚举的完整命名空间路径 |
|
docs/packages.md | 启动新项目或添加依赖时——composer require 命令和包概述 |
|
docs/common-mistakes.md | 在编写任何异步代码之前——19 个以上真实错误及正确/错误示例 |
|
docs/v2-v3.md | 从 AMPHP v2 迁移或遇到 yield/Promise/Coroutine 模式时 |
示例
TimeoutCancellation、DeferredCancellation、CompositeCancellation、传播取消 |
|
examples/sync.md | LocalMutex、LocalSemaphore、LocalParcel、Barrier、LocalKeyedMutex、RateLimitingSemaphore、synchronized() |
|
examples/http-server.md | 最小服务器、带路由参数的路由器、中间件栈、TLS、会话、静态文件、代理设置 |
|
examples/http-client.md | GET/POST、并行请求、ConnectionLimitingPool、拦截器、代理、流式响应体 |
|
examples/websocket.md | 回声服务器、带 drain 模式的仅推送、广播网关、WS 客户端、流式二进制消息 |
|
examples/byte-stream.md | ReadableBuffer、pipe()、GZIP 压缩/解压、Base64 编码/解码、splitLines() |
|
examples/pipelines.md | Queue 背压、Pipeline 操作符(map、filter、tap)、concurrent()、merge()、concat() |
|
examples/parallel.md | Task 接口、带工作池的扇出、IPC 通道进度报告、ChannelException 处理 |
|
examples/database.md | MySQL 连接池、事务、预处理语句;Redis get/set/hash/pubsub/cache |
|
examples/file-io.md | File\read/write/exists/getSize/openFile/listFiles/deleteDirectory/createDirectoryRecursively |
|
examples/cache.md | LocalCache(LRU + TTL)、AtomicCache(compute-if-absent)、PrefixCache、NullCache |
|
examples/interval.md | Interval 重复定时器、enable/disable、weakClosure() 防止 GC 循环、EventLoop::delay/repeat/cancel |
|
examples/testing.md | AsyncTestCase、构造模拟 Request 对象、League\Uri\Http::new()、phpunit CLI 标志 |
工作流
CPU 密集型工作负载跨多个工作进程拆分,带 IPC 进度报告 |
|
workflows/tcp-server.md | 原始 TCP 服务器:回声、自定义二进制协议、TLS 双向认证、优雅关闭 |
模板
WebSocket 处理器的复制粘贴样板:回声、仅推送、广播网关 |
|
templates/parallel-task.php | 带 IPC 进度 + 扇出编排的工作 Task 类的复制粘贴样板 |
脚本
php scripts/http-client-demo.php [url] — 演示 GET、并行请求、ConnectionLimitingPool |
资源
遗留
每个关键 AMPHP 类的一个最小使用示例,按包组织 |