create-rate-limiter

为 PHP 8.4 生成限流模式实现。包含基于令牌桶、滑动窗口和固定窗口三种算法的请求限流逻辑与示例实现,并附带对应的单元测试。

仓库最近推送
GitHub Stars
109
Forks
26
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "create-rate-limiter":https://skillpi.com/zh/skills/deepskill-d8862399ffc1de5727518497622765c492d50c6bfa73e1fddaf78128becbacee。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

为 PHP 8.4 生成限流模式实现。包含基于令牌桶、滑动窗口和固定窗口三种算法的请求限流逻辑与示例实现,并附带对应的单元测试。

0 下载

介绍来源: third_party · dykyi-roman · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: create-rate-limiter
description: Generates Rate Limiter pattern for PHP 8.4. Creates request throttling with token bucket, sliding window, and fixed window algorithms. Includes unit tests.

Rate Limiter Generator

Creates Rate Limiter pattern infrastructure for request throttling and API protection.

When to Use

Scenario Example
API protection Prevent abuse
Resource throttling Database connection limits
Fair usage Per-user request limits
Burst protection Spike handling

Component Characteristics

RateLimiterInterface

  • Common interface for all algorithms
  • Check and consume methods
  • Remaining capacity queries

Algorithms

  • Token Bucket: Smooth rate limiting with burst capacity
  • Sliding Window: Time-based with sliding window log
  • Fixed Window: Simple time-based counter

RateLimitResult

  • Contains allowed/denied status
  • Provides retry-after information
  • Generates HTTP headers

Generation Process

Step 1: Generate Core Components

Path: src/Infrastructure/Resilience/RateLimiter/

  1. RateLimiterInterface.php — Common interface
  2. RateLimitResult.php — Result value object with headers
  3. RateLimitExceededException.php — Exception with retry info
  4. StorageInterface.php — Storage abstraction

Step 2: Choose and Generate Algorithm

Choose based on use case:

  1. TokenBucketRateLimiter.php — For APIs with burst allowance
  2. SlidingWindowRateLimiter.php — For strict per-time limits
  3. FixedWindowRateLimiter.php — For simple rate limiting

Step 3: Generate Storage

  1. RedisStorage.php — Production storage with TTL

Step 4: Generate Tests

  1. {Algorithm}RateLimiterTest.php — Algorithm tests
  2. RateLimitResultTest.php — Result value object tests

File Placement

Component Path
All Classes src/Infrastructure/Resilience/RateLimiter/
Unit Tests tests/Unit/Infrastructure/Resilience/RateLimiter/

Naming Conventions

Component Pattern Example
Interface RateLimiterInterface RateLimiterInterface
Token Bucket TokenBucketRateLimiter TokenBucketRateLimiter
Sliding Window SlidingWindowRateLimiter SlidingWindowRateLimiter
Fixed Window FixedWindowRateLimiter FixedWindowRateLimiter
Result RateLimitResult RateLimitResult
Exception RateLimitExceededException RateLimitExceededException
Test {ClassName}Test TokenBucketRateLimiterTest

Quick Template Reference

RateLimiterInterface

interface RateLimiterInterface
{
    public function attempt(string $key, int $tokens = 1): RateLimitResult;
    public function getRemainingTokens(string $key): int;
    public function getRetryAfter(string $key): ?int;
    public function reset(string $key): void;
}

RateLimitResult

final readonly class RateLimitResult
{
    public static function allowed(int $remaining, int $limit, \DateTimeImmutable $resetsAt): self;
    public static function denied(int $limit, int $retryAfter, \DateTimeImmutable $resetsAt): self;
    public function isAllowed(): bool;
    public function isDenied(): bool;
    public function toHeaders(): array; // X-RateLimit-* headers
}

Usage Example

// Create limiter
$limiter = new TokenBucketRateLimiter(
    capacity: 100,
    refillRate: 10.0, // 10 tokens per second
    clock: $clock,
    storage: new RedisStorage($redis)
);

// Check limit
$result = $limiter->attempt('user:123');

if ($result->isDenied()) {
    throw new RateLimitExceededException(
        key: 'user:123',
        limit: $result->limit,
        retryAfterSeconds: $result->retryAfterSeconds
    );
}

// Add headers to response
foreach ($result->toHeaders() as $name => $value) {
    $response = $response->withHeader($name, (string) $value);
}

Algorithm Comparison

Algorithm Burst Handling Memory Precision Use Case
Token Bucket Good (configurable) Low Medium APIs with burst allowance
Sliding Window Limited High High Strict per-time limits
Fixed Window Poor (boundary issues) Low Low Simple rate limiting

Anti-patterns to Avoid

Anti-pattern Problem Solution
No Redis/Shared Storage Per-instance limits Use shared storage
Missing Headers Client can't adapt Return X-RateLimit-* headers
Single Algorithm Doesn't fit all cases Choose per use case
No Retry-After Client spams Always return retry timing
Synchronous Blocking Thread blocking Use non-blocking check

References

For complete PHP templates and examples, see:

  • references/templates.md — All algorithm implementations
  • references/examples.md — Middleware example and tests

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值75/100

亮点

  • 提供 PHP 8.4 限流模式生成,包含令牌桶、滑动窗口、固定窗口三种算法实现
  • 附带单元测试,适用于 API 防护、资源限流等广泛场景
触发精准90/100

亮点

  • description 明确描述功能,覆盖多种算法和测试,触发词精准完整
结构规范90/100

亮点

  • Phase/步骤清晰(Step 1-4),有出口条件,含端到端示例,frontmatter 完整
执行可靠85/100

亮点

  • 工具来源明确(PHP 8.4),参数正确,覆盖主要异常,不可逆操作有确认机制
证据可验 (前四项均值)85/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。