Phase 6 (SSE 流式处理): - CancellationToken: 客户端断开检测 - SseConcurrencyGuard: 全局+Key 并发槽位管理 - UpstreamStreamClient: 上游 Provider 流式代理 - SseStreamController: SSE 生命周期编排 - RouteMiddleware: 集成 SSE 流式分支 Phase 7 (错误处理+审计): - ErrorMapper: WP_Error → OpenAI 错误格式映射 (14种) - ErrorMiddleware: 异常捕获 + OpenAI 兼容错误响应 - LogMiddleware: 审计日志写入 + 用量统计 upsert Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
64 lines
1.2 KiB
PHP
64 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* Cancellation Token
|
|
*
|
|
* Simple thread-safe cancellation mechanism for SSE streams.
|
|
*
|
|
* @package WPMind\Modules\ApiGateway\Stream
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMind\Modules\ApiGateway\Stream;
|
|
|
|
/**
|
|
* Class CancellationToken
|
|
*
|
|
* Provides a cooperative cancellation signal that can be checked
|
|
* by long-running stream operations to abort gracefully.
|
|
*/
|
|
final class CancellationToken {
|
|
|
|
/**
|
|
* Whether cancellation has been requested.
|
|
*
|
|
* @var bool
|
|
*/
|
|
private bool $cancelled = false;
|
|
|
|
/**
|
|
* Reason for cancellation.
|
|
*
|
|
* @var string
|
|
*/
|
|
private string $reason = '';
|
|
|
|
/**
|
|
* Request cancellation with an optional reason.
|
|
*
|
|
* @param string $reason Human-readable cancellation reason.
|
|
*/
|
|
public function cancel( string $reason = '' ): void {
|
|
$this->cancelled = true;
|
|
$this->reason = $reason;
|
|
}
|
|
|
|
/**
|
|
* Check whether cancellation has been requested.
|
|
*
|
|
* @return bool True if cancelled.
|
|
*/
|
|
public function is_cancelled(): bool {
|
|
return $this->cancelled;
|
|
}
|
|
|
|
/**
|
|
* Get the cancellation reason.
|
|
*
|
|
* @return string Reason string, empty if not cancelled.
|
|
*/
|
|
public function get_reason(): string {
|
|
return $this->reason;
|
|
}
|
|
}
|