gabrielanhaia/php-circuit-breaker 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gabrielanhaia/php-circuit-breaker

Composer 安装命令:

composer require gabrielanhaia/php-circuit-breaker

包简介

Circuit breaker pattern for PHP with multiple storage adapters, event system, and manual override.

README 文档

README

CI Licence PHP Buy Me a Coffee

PHP Circuit Breaker

A robust, production-ready implementation of the Circuit Breaker pattern for PHP. Protect your microservices from cascading failures with configurable thresholds, multiple storage backends, an event system, and manual override capabilities.

How It Works

stateDiagram-v2
    [*] --> Closed
    Closed --> Open : Failures ≥ threshold
    Open --> HalfOpen : Timeout expires
    HalfOpen --> Closed : Successes ≥ threshold
    HalfOpen --> Open : Any failure
Loading
State Description
Closed Normal operation. Requests pass through. Failures are counted.
Open Circuit is tripped. All requests are rejected immediately.
Half-Open Recovery probe. Limited requests are allowed to test if the service has recovered.

Installation

composer require gabrielanhaia/php-circuit-breaker:^3.0

Optional Dependencies

Install only what you need:

# For Redis storage
# Requires ext-redis PHP extension

# For PSR-16 (SimpleCache) storage
composer require psr/simple-cache

# For PSR-6 (Cache) storage
composer require psr/cache

# For PSR-14 event dispatcher bridge
composer require psr/event-dispatcher

Quick Start

use GabrielAnhaia\PhpCircuitBreaker\CircuitBreaker;
use GabrielAnhaia\PhpCircuitBreaker\CircuitBreakerConfig;
use GabrielAnhaia\PhpCircuitBreaker\Storage\InMemoryStorage;

$storage = new InMemoryStorage();
$config  = new CircuitBreakerConfig(
    failureThreshold: 5,
    openTimeout:      30,
);

$cb = new CircuitBreaker($storage, $config);

$service = 'payment-api';

if (!$cb->canPass($service)) {
    // Circuit is open — use fallback
    return cachedResponse();
}

try {
    $result = callPaymentApi();
    $cb->recordSuccess($service);
    return $result;
} catch (\Throwable $e) {
    $cb->recordFailure($service);
    return cachedResponse();
}

Features

  • Multiple storage backends — InMemory, Redis, APCu, Memcached, PSR-6, PSR-16
  • Success threshold — require N consecutive successes before closing a half-open circuit
  • Event system — react to state changes with listeners; optional PSR-14 bridge
  • Manual override — force circuits open/closed for maintenance windows or testing
  • State inspection — query effective circuit state at any time
  • Immutable config — type-safe CircuitBreakerConfig value object
  • Zero required extensions — only ext-redis, ext-apcu, ext-memcached needed if you use those adapters
  • PHPStan level max — fully statically analyzed
  • PHP 8.1+ — uses native enums, readonly properties, and named arguments

Documentation

Topic Description
Configuration Config object, parameter table, exception mode
Storage Adapters All 6 adapters, key scheme, comparison table
Event System Events, sequence diagram, SimpleEventDispatcher, PSR-14 bridge
Manual Override forceState(), clearOverride(), state inspection
Architecture Class diagram, directory structure, request flow

Upgrading from v2

See UPGRADE-3.0.md for a detailed migration guide.

Key Breaking Changes

v2 v3
CircuitStateEnum CircuitState (backing value 'close''closed')
CircuitBreakerAdapter (abstract class) CircuitBreakerStorageInterface
new CircuitBreaker($adapter, $settings) new CircuitBreaker($storage, $config)
Alert interface Event listeners
AdapterException StorageException
CircuitException OpenCircuitException
ext-redis required ext-redis optional (suggested)

Deprecated Methods (Removed in v4)

  • $cb->failed($service) → use $cb->recordFailure($service)
  • $cb->succeed($service) → use $cb->recordSuccess($service)

Development

# Install dependencies
composer install

# Run tests
composer test                              # All tests
vendor/bin/phpunit --testsuite Unit        # Unit tests only
vendor/bin/phpunit --testsuite Integration # Integration tests only

# Static analysis
composer phpstan

# Code style
composer cs-check    # Check for violations
composer cs-fix      # Auto-fix violations

CI

GitHub Actions runs on PHP 8.1, 8.2, 8.3, 8.4 with three jobs:

  • Tests — unit + integration (with Redis service)
  • Static Analysis — PHPStan level max
  • Code Style — PHP-CS-Fixer (PER-CS + PHP 8.1 migration)

Support

If this library helps you, consider buying me a coffee:

Buy Me a Coffee

License

MIT — see LICENSE for details.

Created by Gabriel Anhaia | Buy Me a Coffee

gabrielanhaia/php-circuit-breaker 适用场景与选型建议

gabrielanhaia/php-circuit-breaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2020 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gabrielanhaia/php-circuit-breaker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gabrielanhaia/php-circuit-breaker 我们能提供哪些服务?
定制开发 / 二次开发

基于 gabrielanhaia/php-circuit-breaker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 7
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-22