Phase 4 (认证+预算+限流): - AuthMiddleware: Bearer Key (API) / Cookie+Nonce (管理端) - BudgetMiddleware: 月度预算检查 (api_key_usage 表) - QuotaMiddleware: RPM/TPM 双维度限流 - RateLimit 子系统: Redis 滑动窗口 + Transient 降级 Phase 5 (路由+转换): - ModelMapper: 18 模型映射 + 别名 + auto 路由 - RequestTransformer: OpenAI → WPMind (体积/token 上限) - ResponseTransformer: WPMind → OpenAI 格式 - RouteMiddleware: 分发到 PublicAPI - RequestTransformMiddleware / ResponseTransformMiddleware Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* Rate Store Interface
|
|
*
|
|
* Contract for sliding-window rate limit backends.
|
|
*
|
|
* @package WPMind\Modules\ApiGateway\RateLimit
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMind\Modules\ApiGateway\RateLimit;
|
|
|
|
/**
|
|
* Interface RateStoreInterface
|
|
*
|
|
* Implementations provide atomic consume/rollback for rate limiting.
|
|
*/
|
|
interface RateStoreInterface {
|
|
|
|
/**
|
|
* Attempt to consume capacity from a rate-limit bucket.
|
|
*
|
|
* @param string $key Bucket identifier (e.g. "rpm:{key_id}").
|
|
* @param int $window_sec Sliding window size in seconds.
|
|
* @param int $cost Units to consume (1 for RPM, token count for TPM).
|
|
* @param int $limit Maximum allowed units per window.
|
|
* @param string $rid Unique request ID for rollback support.
|
|
* @param int $now Current Unix timestamp.
|
|
* @return RateStoreResult
|
|
*/
|
|
public function consume( string $key, int $window_sec, int $cost, int $limit, string $rid, int $now ): RateStoreResult;
|
|
|
|
/**
|
|
* Roll back a previously consumed entry by request ID.
|
|
*
|
|
* @param string $key Bucket identifier.
|
|
* @param string $rid Request ID to remove.
|
|
*/
|
|
public function rollback( string $key, string $rid ): void;
|
|
}
|