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

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

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

prestashop/circuit-breaker

Composer 安装命令:

composer require prestashop/circuit-breaker

包简介

A circuit breaker implementation for PHP

README 文档

README

codecov PHPStan Psalm Build

Main principles

circuit breaker

This library is compatible with PHP 7.4+.

Installation

composer require prestashop/circuit-breaker

Use

Symfony Http Client and Guzzle Client implementations

By default, Circuit Breaker use the Symfony Http Client library, and all the client options are described in the official documentation.

For retro-compatibility, we let you use Guzzle Client instead of Symfony Http Client. To use Guzzle, you need to set the Guzzle client with setClient() of the settings factory, like this example below:

use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
use PrestaShop\CircuitBreaker\FactorySettings;
use PrestaShop\CircuitBreaker\Client\GuzzleClient

$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$factorySettings = new FactorySettings(2, 0.1, 10);
$factorySettings->setClient(new GuzzleHttpClient());

$circuitBreaker = $circuitBreakerFactory->create($factorySettings);

Be aware, that the client options depend on the client implementation you choose!

For the Guzzle implementation, the Client options are described in the HttpGuzzle documentation.

Simple Circuit Breaker

You can use the factory to create a simple circuit breaker.

By default, you need to define 3 parameters for the circuit breaker:

  • the failures: define how many times we try to access the service;
  • the timeout: define how much time we wait before consider the service unreachable;
  • the threshold: define how much time we wait before trying to access again the service (once it is considered unreachable);

The fallback callback will be used if the distant service is unreachable when the Circuit Breaker is Open (means "is used" if the service is unreachable).

You'd better return the same type of response expected from your distant call.

use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
use PrestaShop\CircuitBreaker\FactorySettings;

$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$circuitBreaker = $circuitBreakerFactory->create(new FactorySettings(2, 0.1, 10));

$fallbackResponse = function () {
    return '{}';
};

$response = $circuitBreaker->call('https://api.domain.com', [], $fallbackResponse);

If you don't specify any fallback, by default the circuit breaker will return an empty string.

use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
use PrestaShop\CircuitBreaker\FactorySettings;

$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$circuitBreaker = $circuitBreakerFactory->create(new FactorySettings(2, 0.1, 10));
$response = $circuitBreaker->call('https://unreacheable.api.domain.com', []); // $response == ''

You can also define the client options (or even set your own client if you prefer).

use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
use PrestaShop\CircuitBreaker\FactorySettings;

$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$settings = new FactorySettings(2, 0.1, 10);
$settings->setClientOptions(['method' => 'POST']);
$circuitBreaker = $circuitBreakerFactory->create($settings);
$response = $circuitBreaker->call('https://api.domain.com/create/user', ['body' => ['firstname' => 'John', 'lastname' => 'Doe']]);

Advanced Circuit Breaker

If you need more control on your circuit breaker, you should use the AdvancedCircuitBreaker which manages more features:

  • the stripped failures: define how many times we try to access the service when the circuit breaker is Half Open (when it retires to reach the service after it was unreachable);
  • the stripped timeout: define how much time we wait before consider the service unreachable (again in Half open state);
  • the storage: used to store the circuit breaker states and transitions. By default it's an SimpleArray so if you want to "cache" the fact that your service is unreachable you should use a persistent storage;
  • the transition dispatcher: used if you need to subscribe to transition events (ex: a dispatcher based on Symfony EventDispatcher is available)

Storage

use Doctrine\Common\Cache\FilesystemCache;
use PrestaShop\CircuitBreaker\AdvancedCircuitBreakerFactory;
use PrestaShop\CircuitBreaker\FactorySettings;
use PrestaShop\CircuitBreaker\Storage\DoctrineCache;

$circuitBreakerFactory = new AdvancedCircuitBreakerFactory();
$settings = new FactorySettings(2, 0.1, 60); //60 seconds threshold

//Once the circuit breaker is open, the fallback response will be returned instantly during the next 60 seconds
//Since the state is persisted even other requests/processes will be aware that the circuit breaker is open
$doctrineCache = new FilesystemCache(_PS_CACHE_DIR_ . '/addons_category');
$storage = new DoctrineCache($doctrineCache);
$settings->setStorage($storage);

$circuitBreaker = $circuitBreakerFactory->create($settings);
$response = $circuitBreaker->call('https://unreachable.api.domain.com/create/user', []);

Tests

composer test

Code quality

composer cs-fix && composer phpcb && composer psalm && composer phpcs

We also use PHPQA to check the Code quality during the CI management of the contributions.

If you want to use it (using Docker):

docker run --rm -u $UID -v $(pwd):/app eko3alpha/docker-phpqa --report --ignoredDirs vendor,tests

If you want to use it (using Composer):

composer global require edgedesign/phpqa=v1.20.0 --update-no-dev
phpqa --report --ignoredDirs vendor,tests

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

prestashop/circuit-breaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.07M 次下载、GitHub Stars 达 36, 最近一次更新时间为 2019 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.07M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 20
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 17
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-21