PHPackages                             prestashop/circuit-breaker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. prestashop/circuit-breaker

ActiveLibrary[HTTP &amp; Networking](/categories/http)

prestashop/circuit-breaker
==========================

A circuit breaker implementation for PHP

v4.0.3(1y ago)367.4M—6.1%133MITPHPPHP &gt;=7.2.34

Since Jan 10Pushed 1y ago17 watchersCompare

[ Source](https://github.com/PrestaShop/circuit-breaker)[ Packagist](https://packagist.org/packages/prestashop/circuit-breaker)[ RSS](/packages/prestashop-circuit-breaker/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (9)Dependencies (8)Versions (13)Used By (3)

Circuit Breaker, an implementation for resilient PHP applications
=================================================================

[](#circuit-breaker-an-implementation-for-resilient-php-applications)

[![codecov](https://camo.githubusercontent.com/0a4b1a128b25f3e784b17d6887212aa7f7d09c31f52355b525edc13d7e310dee/68747470733a2f2f636f6465636f762e696f2f67682f50726573746153686f702f636972637569742d627265616b65722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/PrestaShop/circuit-breaker)[![PHPStan](https://camo.githubusercontent.com/8f23aa0da0b5c240ca42d6656ff26c49534e74e9c1a69d0b401797b42760688f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c253230372d627269676874677265656e2e7376673f7374796c653d666c6174266c6f676f3d706870)](https://shields.io/#/)[![Psalm](https://camo.githubusercontent.com/96b905dc00aa280becfa54dd6358d21c63dd7c8c7a7ff93810ccff917e5954a7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5073616c6d2d4c6576656c2532304d61782d627269676874677265656e2e7376673f7374796c653d666c6174266c6f676f3d706870)](https://shields.io/#/)[![Build](https://github.com/PrestaShop/circuit-breaker/actions/workflows/php.yml/badge.svg)](https://github.com/PrestaShop/circuit-breaker/actions/workflows/php.yml)

Main principles
---------------

[](#main-principles)

[![circuit breaker](https://user-images.githubusercontent.com/1247388/49721725-438bd700-fc63-11e8-8498-82ca681b15fb.png)](https://user-images.githubusercontent.com/1247388/49721725-438bd700-fc63-11e8-8498-82ca681b15fb.png)

This library is compatible with PHP 7.4+.

Installation
------------

[](#installation)

```
composer require prestashop/circuit-breaker

```

Use
---

[](#use)

### Symfony Http Client and Guzzle Client implementations

[](#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](https://symfony.com/doc/current/http_client.html).

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](http://docs.guzzlephp.org/en/stable/index.html).

### Simple Circuit Breaker

[](#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

[](#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

[](#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
-----

[](#tests)

```
composer test

```

Code quality
------------

[](#code-quality)

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

```

We also use [PHPQA](https://github.com/EdgedesignCZ/phpqa#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

```

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity57

Moderate usage in the ecosystem

Community30

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~267 days

Recently: every ~497 days

Total

9

Last Release

544d ago

Major Versions

v1.1.1 → v2.0.02019-03-29

v2.0.0 → v3.0.02019-06-13

v3.0.0 → v4.0.02021-04-20

PHP version history (3 changes)v1.0PHP &gt;=5.6

v4.0.2PHP &gt;=7.2.5

v4.0.3PHP &gt;=7.2.34

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15106407?v=4)[Jarvis](/maintainers/ps-jarvis)[@ps-jarvis](https://github.com/ps-jarvis)

![](https://avatars.githubusercontent.com/u/1009343?v=4)[Pablo Borowicz](/maintainers/eternoendless)[@eternoendless](https://github.com/eternoendless)

---

Top Contributors

[![jolelievre](https://avatars.githubusercontent.com/u/13801017?v=4)](https://github.com/jolelievre "jolelievre (36 commits)")[![mickaelandrieu](https://avatars.githubusercontent.com/u/1247388?v=4)](https://github.com/mickaelandrieu "mickaelandrieu (22 commits)")[![PierreRambaud](https://avatars.githubusercontent.com/u/1462701?v=4)](https://github.com/PierreRambaud "PierreRambaud (17 commits)")[![boherm](https://avatars.githubusercontent.com/u/18699562?v=4)](https://github.com/boherm "boherm (7 commits)")[![Progi1984](https://avatars.githubusercontent.com/u/1533248?v=4)](https://github.com/Progi1984 "Progi1984 (7 commits)")[![matks](https://avatars.githubusercontent.com/u/3830050?v=4)](https://github.com/matks "matks (3 commits)")[![atomiix](https://avatars.githubusercontent.com/u/2168836?v=4)](https://github.com/atomiix "atomiix (2 commits)")[![Hlavtox](https://avatars.githubusercontent.com/u/6097524?v=4)](https://github.com/Hlavtox "Hlavtox (2 commits)")[![matthieu-rolland](https://avatars.githubusercontent.com/u/1784781?v=4)](https://github.com/matthieu-rolland "matthieu-rolland (1 commits)")[![kpodemski](https://avatars.githubusercontent.com/u/2137763?v=4)](https://github.com/kpodemski "kpodemski (1 commits)")

---

Tags

circuit-breakerguzzlehacktoberfestlibraryphpresiliency

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/prestashop-circuit-breaker/health.svg)

```
[![Health](https://phpackages.com/badges/prestashop-circuit-breaker/health.svg)](https://phpackages.com/packages/prestashop-circuit-breaker)
```

###  Alternatives

[async-aws/core

Core package to integrate with AWS. This is a lightweight AWS SDK provider by AsyncAws.

10139.5M66](/packages/async-aws-core)[fresh/centrifugo-bundle

Provides communication with web-socket server Centrifugo in Symfony applications.

83375.3k](/packages/fresh-centrifugo-bundle)[bitrix24/b24phpsdk

An official PHP library for the Bitrix24 REST API

9230.2k4](/packages/bitrix24-b24phpsdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
