acc-stability-patterns-knowledge
1
总安装量
1
周安装量
#55042
全站排名
安装命令
npx skills add https://github.com/dykyi-roman/awesome-claude-code --skill acc-stability-patterns-knowledge
Agent 安装分布
opencode
1
claude-code
1
Skill 文档
Stability Patterns Knowledge Base
Quick reference for resilience and fault tolerance patterns in PHP applications.
Core Patterns Overview
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â STABILITY PATTERNS â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ â
â â REQUEST FLOW â â
â â â â
â â Client âââ¶ Rate Limiter âââ¶ Circuit Breaker âââ¶ Service â â
â â â â â â â â
â â â Throttle Monitor State Actual â â
â â â requests Open/Closed work â â
â â â â â â â â
â â â â¼ â¼ â¼ â â
â â â ââââââââââ ââââââââââ ââââââââââ â â
â â â âBulkheadâ â Retry â âTimeout â â â
â â â âIsolate â âPattern â âControl â â â
â â â ââââââââââ ââââââââââ ââââââââââ â â
â ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â Pattern â Purpose â Protects Against â
â ââââââââââââââââââ¼âââââââââââââââââââââââââââââ¼âââââââââââââââââââââââââââ â
â Rate Limiter â Throttle request rate â DDoS, overload, abuse â
â Circuit Breaker â Fail fast on failures â Cascading failures â
â Retry â Retry transient failures â Temporary outages â
â Bulkhead â Isolate resources â Resource exhaustion â
â Timeout â Limit wait time â Slow dependencies â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Pattern Relationships
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PATTERN INTERACTION â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â ââââââââââââââââââââââ â
â â Rate Limiter â â
â â (Entry Point) â â
â âââââââââââ¬âââââââââââ â
â â â
â â¼ â
â ââââââââââââââââââââââ â
â â Bulkhead â â
â â (Resource Limits) â â
â âââââââââââ¬âââââââââââ â
â â â
â âââââââââââââââââââââââââ¼ââââââââââââââââââââââââ â
â â â â â
â â¼ â¼ â¼ â
â ââââââââââââââââ ââââââââââââââââ ââââââââââââââââ â
â â Service A â â Service B â â Service C â â
â â ââââââââââ â â ââââââââââ â â ââââââââââ â â
â â âCircuit â â â âCircuit â â â âCircuit â â â
â â âBreaker â â â âBreaker â â â âBreaker â â â
â â âââââ¬âââââ â â âââââ¬âââââ â â âââââ¬âââââ â â
â â â â â â â â â â â
â â âââââ¼âââââ â â âââââ¼âââââ â â âââââ¼âââââ â â
â â â Retry â â â â Retry â â â â Retry â â â
â â âPattern â â â âPattern â â â âPattern â â â
â â ââââââââââ â â ââââââââââ â â ââââââââââ â â
â ââââââââââââââââ ââââââââââââââââ ââââââââââââââââ â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Quick Reference
Circuit Breaker States
| State | Behavior | Transitions To |
|---|---|---|
| Closed | Requests pass through, failures counted | Open (on threshold) |
| Open | Requests fail fast, no calls to service | Half-Open (after timeout) |
| Half-Open | Limited requests allowed for testing | Closed (on success) / Open (on failure) |
Retry Backoff Strategies
| Strategy | Formula | Use Case |
|---|---|---|
| Fixed | delay |
Simple cases, known recovery time |
| Linear | delay * attempt |
Gradual increase |
| Exponential | delay * 2^(attempt-1) |
Unknown recovery, default choice |
| Exponential + Jitter | exponential ± random |
High concurrency, prevents thundering herd |
Rate Limiter Algorithms
| Algorithm | Precision | Memory | Burst Handling |
|---|---|---|---|
| Token Bucket | Medium | Low | Allows bursts |
| Sliding Window | High | Medium | Smooth limiting |
| Fixed Window | Low | Low | Edge bursts |
| Leaky Bucket | High | Low | No bursts |
Bulkhead Types
| Type | Isolation | Use Case |
|---|---|---|
| Semaphore | Thread/request count | Single-process apps |
| Thread Pool | Dedicated threads | CPU-bound work |
| Queue-based | Request queue | Async processing |
| Distributed | Redis/shared state | Multi-instance apps |
PHP Implementation Patterns
Circuit Breaker with PSR Clock
<?php
declare(strict_types=1);
namespace Infrastructure\Resilience;
use Psr\Clock\ClockInterface;
final class CircuitBreaker
{
private CircuitState $state = CircuitState::Closed;
private int $failures = 0;
private ?\DateTimeImmutable $openedAt = null;
public function __construct(
private readonly string $name,
private readonly int $failureThreshold,
private readonly int $openTimeoutSeconds,
private readonly ClockInterface $clock
) {}
public function execute(callable $operation, ?callable $fallback = null): mixed
{
if (!$this->isAvailable()) {
return $fallback ? $fallback() : throw new CircuitOpenException($this->name);
}
try {
$result = $operation();
$this->recordSuccess();
return $result;
} catch (\Throwable $e) {
$this->recordFailure();
throw $e;
}
}
private function isAvailable(): bool
{
if ($this->state === CircuitState::Closed) return true;
if ($this->state === CircuitState::Open) {
if ($this->hasTimeoutElapsed()) {
$this->state = CircuitState::HalfOpen;
return true;
}
return false;
}
return true;
}
}
Retry with Exponential Backoff
<?php
declare(strict_types=1);
namespace Infrastructure\Resilience;
final readonly class RetryExecutor
{
public function execute(
callable $operation,
int $maxAttempts = 3,
int $baseDelayMs = 100
): mixed {
$attempt = 0;
$lastException = null;
while ($attempt < $maxAttempts) {
try {
return $operation();
} catch (\Throwable $e) {
$lastException = $e;
$attempt++;
if ($attempt < $maxAttempts) {
$delay = $baseDelayMs * (2 ** ($attempt - 1));
$jitter = random_int(0, (int)($delay * 0.3));
usleep(($delay + $jitter) * 1000);
}
}
}
throw $lastException;
}
}
Token Bucket Rate Limiter
<?php
declare(strict_types=1);
namespace Infrastructure\Resilience;
final class TokenBucketRateLimiter
{
private float $tokens;
private int $lastRefill;
public function __construct(
private readonly int $capacity,
private readonly float $refillRate,
private readonly \Redis $redis,
private readonly string $key
) {
$this->tokens = $capacity;
$this->lastRefill = time();
}
public function attempt(): bool
{
$this->refill();
if ($this->tokens >= 1) {
$this->tokens--;
return true;
}
return false;
}
private function refill(): void
{
$now = time();
$elapsed = $now - $this->lastRefill;
$this->tokens = min(
$this->capacity,
$this->tokens + ($elapsed * $this->refillRate)
);
$this->lastRefill = $now;
}
}
Common Violations Quick Reference
| Violation | Where to Look | Severity |
|---|---|---|
| No timeout on external calls | HTTP clients, DB queries | Critical |
| Retry without backoff | Retry implementations | Warning |
| No circuit breaker on external services | API clients, adapters | Critical |
| Unbounded connection pools | Database, HTTP pools | Warning |
| No fallback strategy | Circuit breaker usage | Warning |
| Retry non-idempotent operations | Command handlers | Critical |
| Rate limiting only in-memory | Multi-instance apps | Warning |
| No jitter in retry | High-concurrency systems | Warning |
Detection Patterns
# Find resilience implementations
Glob: **/Resilience/**/*.php
Glob: **/CircuitBreaker/**/*.php
Grep: "CircuitBreaker|RateLimiter|Retry" --glob "**/*.php"
# Check for proper timeout usage
Grep: "CURLOPT_TIMEOUT|timeout|setTimeout" --glob "**/Http/**/*.php"
# Detect retry patterns
Grep: "retry|backoff|exponential" --glob "**/*.php"
# Find rate limiting
Grep: "RateLimiter|throttle|TokenBucket" --glob "**/*.php"
# Check for bulkhead patterns
Grep: "Semaphore|Bulkhead|maxConcurrent" --glob "**/*.php"
# Detect missing patterns
Grep: "->request\(|curl_exec|file_get_contents" --glob "**/Infrastructure/**/*.php"
Configuration Guidelines
Circuit Breaker Settings
| Service Type | Failure Threshold | Open Timeout | Success Threshold |
|---|---|---|---|
| Critical API | 3-5 | 30-60s | 3-5 |
| Background Job | 5-10 | 60-120s | 2-3 |
| Internal Service | 3-5 | 15-30s | 2-3 |
| Database | 2-3 | 10-20s | 1-2 |
Retry Configuration
| Operation Type | Max Attempts | Base Delay | Max Delay |
|---|---|---|---|
| HTTP API Call | 3 | 100ms | 10s |
| Database Query | 3 | 50ms | 5s |
| Message Queue | 5 | 1s | 60s |
| File Operation | 2 | 10ms | 100ms |
Rate Limiter Settings
| Endpoint Type | Rate | Window | Burst |
|---|---|---|---|
| Public API | 100/min | 1 min | 20 |
| Authenticated API | 1000/min | 1 min | 100 |
| Admin API | 10000/min | 1 min | 1000 |
| Webhook | 60/min | 1 min | 10 |
Integration Points
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â INFRASTRUCTURE LAYER â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â src/Infrastructure/ â
â âââ Resilience/ â
â â âââ CircuitBreaker/ â
â â â âââ CircuitBreaker.php â
â â â âââ CircuitBreakerConfig.php â
â â â âââ CircuitBreakerRegistry.php â
â â â âââ CircuitState.php â
â â âââ Retry/ â
â â â âââ RetryExecutor.php â
â â â âââ RetryPolicy.php â
â â â âââ BackoffStrategy.php â
â â âââ RateLimiter/ â
â â â âââ RateLimiterInterface.php â
â â â âââ TokenBucketRateLimiter.php â
â â â âââ SlidingWindowRateLimiter.php â
â â âââ Bulkhead/ â
â â âââ BulkheadInterface.php â
â â âââ SemaphoreBulkhead.php â
â â âââ BulkheadRegistry.php â
â â â
â âââ Http/ â
â â âââ ResilientHttpClient.php âââ Uses CircuitBreaker + Retry â
â â âââ Middleware/ â
â â âââ RateLimitMiddleware.php â
â â â
â âââ Payment/ â
â âââ PaymentGatewayAdapter.php âââ Uses CircuitBreaker + Bulkhead â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
References
For detailed information, load these reference files:
references/circuit-breaker.mdâ Circuit Breaker implementation detailsreferences/retry-patterns.mdâ Retry strategies and backoff algorithmsreferences/rate-limiting.mdâ Rate limiting algorithms and configurationsreferences/bulkhead.mdâ Bulkhead isolation patterns
Assets
assets/report-template.mdâ Structured audit report template