承接 omidrezasalari/circuit-breaker-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

omidrezasalari/circuit-breaker-bundle

Composer 安装命令:

composer require omidrezasalari/circuit-breaker-bundle

包简介

A Symfony bundle implementing the Circuit Breaker pattern.

README 文档

README

This package is a Symfony bundle that implements the Circuit Breaker pattern. It helps protect your services from repeated requests when failures occur. The package is designed to allow you to easily change the storage mechanism (e.g., Redis, APCu, or custom implementations).

Features

  • Circuit Breaker Pattern: Prevents repeated requests after multiple failures.
  • Flexible Storage: By default, it uses APCu, but you can switch to Redis or any custom storage implementation.
  • Configurable: Dynamically configure failure thresholds and timeout periods via Symfony configuration files.
  • Symfony 6+ Compatible: Integrates seamlessly with modern Symfony projects.

Installation

1. APCu Implementation (Optimized and Persistent in Memory)

APCu is an internal PHP cache that stores data in shared memory and can be accessed across different requests on a server.

1.1 Install and Enable APCu

Before using APCu as a storage mechanism for the Circuit Breaker bundle, ensure that APCu is installed and enabled on your server.

1.2 Check if APCu is installed:

php -m | grep apcu

If it's not installed, you can install it with the following commands:

  • For Linux (Ubuntu/Debian):
sudo apt install php-apcu
  • For macOS:
brew install php-apcu

1.3 Enable APCu for CLI (Command Line Interface):

Edit your php.ini file to enable APCu for the CLI:

apc.enable_cli = 1

2. Install the package via Composer, run the following command:

composer require omidrezasalari/circuit-breaker-bundle

After installing, register the bundle in your config/bundles.php:

return [
    // Other bundles...
    Omidrezasalari\CircuitBreakerBundle\CircuitBreakerBundle::class => ['all' => true],
];

Configuration

The circuit breaker bundle uses a configurable failure threshold and timeout period. You can configure these parameters in your config/services.yaml.

#.env
REDIS_HOST=localhost
REDIS_PORT=6379
FAILURE_THRESHOLD=5
TIMEOUT_PERIOD=60
# config/services.yaml
parameters:
  redis_host: '%env(REDIS_HOST)%'
  redis_port: '%env(REDIS_PORT)%'
  circuit_breaker.failure_threshold: '%env(FAILURE_THRESHOLD)%'
  circuit_breaker.timeout_period: '%env(TIMEOUT_PERIOD)%'

services:
  Omidrezasalari\CircuitBreakerBundle\Service\RedisStorage:
    arguments:
      $host: '%redis_host%'
      $port: '%redis_port%'

  Omidrezasalari\CircuitBreakerBundle\Service\ApcuStorage: ~

  Omidrezasalari\CircuitBreakerBundle\Service\CircuitBreaker:
    arguments:
      $failureTreshHold: '%circuit_breaker.failure_threshold%'
      $timeoutPeriod: '%circuit_breaker.timeout_period%'

Package Configuration File (circuit_breaker.yaml):

This file is placed under the config/packages/ directory and allows users to define custom configurations for the package. The file looks like this

# config/packages/circuit_breaker.yaml
circuit_breaker:
  storage_service: 'Omidrezasalari\CircuitBreakerBundle\Service\ApcuStorage'
  failure_threshold: 5
  timeout_period: 60

Configuration Details:

Storage Service:

By default, the storage_service is set to ApcuStorage. If you prefer to use RedisStorage, simply change this value in circuit_breaker.yaml:

circuit_breaker:
  storage_service: 'Omidrezasalari\CircuitBreakerBundle\Service\RedisStorage'
  failure_threshold: 5
  timeout_period: 60 

failure_threshold and timeout_period parameters:

These values control how many failures are allowed before the circuit breaker trips and for how long it stays open.

Custom Storage

The bundle comes with a default ApcuStorage implementation. If you want to use a different storage solution, implement the StorageInterface and pass it to the CircuitBreaker service.

namespace App\Service;

use Omidrezasalari\CircuitBreakerBundle\Service\StorageInterface;

class CustomStorage implements StorageInterface
{
    // Implement required methods: get, set, increment, expire
}

Usage

Here’s an example of how to use the CircuitBreaker service:

use Omidrezasalari\CircuitBreakerBundle\Service\CircuitBreaker;

class YourService
{
    private CircuitBreaker $circuitBreaker;

    public function __construct(CircuitBreaker $circuitBreaker)
    {
        $this->circuitBreaker = $circuitBreaker;
    }

    public function someOperation()
    {
        if ($this->circuitBreaker->isOpen('your_service')) {
            throw new \Exception("Circuit breaker is open. Operation not allowed.");
        }

        try {
            // Perform the operation
        } catch (\Exception $e) {
            $this->circuitBreaker->attemptFailure('your_service');
            throw $e;
        }

        // If operation succeeds
        $this->circuitBreaker->attemptSuccess('your_service');
    }
}

API

isOpen(string $serviceName): bool

Checks whether the circuit breaker for the given service is open. Returns true if open, false if not.

attemptSuccess(string $serviceName): void

Called when a service operation is successful. Resets the failure count and closes the circuit breaker.

attemptFailure(string $serviceName): void

Called when a service operation fails. Increments the failure count. If the failure threshold is reached, the circuit breaker will open.

Running Tests

To run the tests for the bundle, you can use PHPUnit:

vendor/bin/phpunit 

Contributing

If you'd like to contribute to the development of this bundle, feel free to fork the repository, create a branch, and submit a pull request. Please ensure your code follows the coding standards and includes tests for any new features or bug fixes.

License

This bundle is open-source and available under the MIT License.

omidrezasalari/circuit-breaker-bundle 适用场景与选型建议

omidrezasalari/circuit-breaker-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-01-23