PHPackages                             ejsmont-artur/php-circuit-breaker-bundle - 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. ejsmont-artur/php-circuit-breaker-bundle

ActiveSymfony-bundle

ejsmont-artur/php-circuit-breaker-bundle
========================================

PHP Circuit Breaker Bundle - Symfony 2 integration

0.1.5(13y ago)13782[1 issues](https://github.com/ejsmont-artur/php-circuit-breaker-bundle/issues)MITPHPPHP &gt;=5.3.0

Since Mar 16Pushed 10y ago4 watchersCompare

[ Source](https://github.com/ejsmont-artur/php-circuit-breaker-bundle)[ Packagist](https://packagist.org/packages/ejsmont-artur/php-circuit-breaker-bundle)[ Docs](https://github.com/ejsmont-artur/php-circuit-breaker-bundle)[ RSS](/packages/ejsmont-artur-php-circuit-breaker-bundle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (7)Used By (0)

What is php-circuit-breaker-bundle
==================================

[](#what-is-php-circuit-breaker-bundle)

[![Build Status](https://camo.githubusercontent.com/83e7ee6abe040d670056248d08de27808f91dcd389e8f5738fbe3b88c7718929/68747470733a2f2f7472617669732d63692e6f72672f656a736d6f6e742d61727475722f7068702d636972637569742d627265616b65722d62756e646c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/ejsmont-artur/php-circuit-breaker-bundle)

[![knpbundles.com](https://camo.githubusercontent.com/56f22efcb735cb1f608cc76c744f67f0b9cf71ebc5cbc58ba7a5085462f71fc3/687474703a2f2f6b6e7062756e646c65732e636f6d2f656a736d6f6e742d61727475722f7068702d636972637569742d627265616b65722d62756e646c652f62616467652d73686f7274)](http://knpbundles.com/ejsmont-artur/php-circuit-breaker-bundle)

php-circuit-breaker-bundle is a [Symfony 2](https://github.com/symfony/symfony) bundle, providing easy integration of [php-circuit-breaker](https://github.com/ejsmont-artur/php-circuit-breaker) component.

[php-circuit-breaker](https://github.com/ejsmont-artur/php-circuit-breaker) is the core package providing a generic PHP implementation of [circuit breaker pattern](http://artur.ejsmont.org/blog/circuit-breaker). This bundle wraps it up and makes easier to use with [Symfony 2](https://github.com/symfony/symfony) framework. Bundle uses service.xml to configure default services. It also integrates with Doctrine/Cache to allow you to use any cache backend (in case you were already using Doctrine/Cache).

Motivation &amp; Benefits
=========================

[](#motivation--benefits)

- Easy use of circuit breaker withing [Symfony 2](https://github.com/symfony/symfony) applications.

Installation
============

[](#installation)

Since [Symfony 2](https://github.com/symfony/symfony) uses [Composer](http://getcomposer.org/), all you have to do is add a require dependency to your composer.json

```
"require": {
    "ejsmont-artur/php-circuit-breaker-bundle": "0.1.*"
},

```

Then you can override defaults of threshold and timeout in your application services.yaml

```
parameters:
    # Allowed amount of failures before marking service as unavailable
    ejsmont_circuit_breaker.threshold: 3
    # how many seconds should we wait before allowing a single request
    ejsmont_circuit_breaker.retry_timeout: 5

```

After that you should update composer dependencies and you are good to go.

Examples
========

[](#examples)

Below you can see a few ways of obtaining instances of circuit breaker component. You can also see how to use it once you get an instance. For more documentation please see [php-circuit-breaker](https://github.com/ejsmont-artur/php-circuit-breaker) page.

Example 1 - default APC storage
-------------------------------

[](#example-1---default-apc-storage)

This is the simplest example as you use defaults for all settings and default APC storage. Circuit breaker status information will be serialised into APC cache.

```
$circuitBreaker = $this->get('apcCircuitBreaker');

```

Yes, that is it. You can use predefined service called apcCircuitBreaker and you do not need any settings. It will use static factory and keep returning the same instance during script run.

Example 2 - configuration via dependency injection
--------------------------------------------------

[](#example-2---configuration-via-dependency-injection)

Beside of "apcCircuitBreaker" service that uses APC you can use "circuitBreaker" service which is configurable and allows you to inject any doctrine cache instance.

In the example below i use "circuitBreakerCacheBackend" service to override the default behaviour of "circuitBreaker" service. Here i am using memcached but it could be any doctrine cache instance. This service affects behaviour of "circuitBreaker" only, "apcCircuitBreaker" service uses its own APC cache instance.

Configure as needed in service.yaml of your app:

```
services:
    circuitBreakerCacheBackend:
        class: Doctrine\Common\Cache\MemcachedCache
        calls:
          -   [setMemcached, ["@memcachedInstance"]]
    memcachedInstance:
        class: Memcached
        calls:
            - [addServer, ['127.0.0.1', 11211, 1]]

```

Then in your code you can use the configurable doctrine cache instance like below:

```
 $circuitBreaker = $this->get('circuitBreaker');

```

Example 3 - manual composition
------------------------------

[](#example-3---manual-composition)

If you wanted to do it for some reason you can also create instance of circuit breaker by hand. In this example we use Doctrine\\Cache adapter so all you need to provide is the cache instance. In this case, just to mix it up, we have decided to use file cache.

```
$fileCache = new \Doctrine\Common\Cache\FilesystemCache('/tmp/cache/', '.cache');
$circuitBreaker = Factory::getDoctrineCacheInstance($fileCache);

```

Example of how to use an instance
---------------------------------

[](#example-of-how-to-use-an-instance)

See more details of Circuit Breaker pattern on [php-circuit-breaker](https://github.com/ejsmont-artur/php-circuit-breaker)github page and my [blog posts on circuit breaker](http://artur.ejsmont.org/blog/circuit-breaker)

In short, once you get instance of circuit breaker, you can ask it if particular service is available or not. Circuit breaker will check it's status metrics and give you response based on its previous records. After a successful connection to the service you should tell Circuit Breaker that it went ok. In case of service failure or timeout, you should report failure to circuit breaker.

This way Circuit breaker "learns" what is the current status of each service (names are arbitrary strings). You can define threshold and retry timeout to allow single request from time to time in case service got fixed.

```
if ($circuitBreaker->isAvailable("UserProfileService1")) {
    try{
        // do something useful with the service
        $circuitBreaker->reportSuccess('UserProfileService1');
    }catch(ServiceCallFailedServiceDown $e){
        // if service is down report it back to circuit breaker
        $circuitBreaker->reportFailure('UserProfileService1');
        // handle as temporarily unavailable (or remove some features)
    }
}else{
    // handle as temporarily unavailable (or remove some features)
}

```

Running tests
-------------

[](#running-tests)

- Tests are run via PHPUnit It is assumed to be installed via PEAR.
- Tests can be ran using phpunit alone or via ant build targets.
- Tests require all dependencies to be present as bundle is expected to by used only in symfony2 apps.
- The "ci" target generate code coverage repor, "phpunit" target does not.

If you dont have composer, get it and download dependencies (creates /vendor folder)

```
curl -s http://getcomposer.org/installer | php
php composer.phar update

```

You can run all tests using ant:

```
ant phpunit

```

You can run tests, generate coverage and docs:

```
ant ci

```

You can run selected test case by running:

```
cd tests
phpunit Unit/Ejsmont/CircuitBreakerBundle/Storage/DoctrineCacheAdapterTest.php

```

Author
------

[](#author)

- Artur Esjmont () via

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~1 days

Total

6

Last Release

4800d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/653af0e6b24b08aeaa34911f9ca93b9fc041cb74e8e141a50bbab283f29895db?d=identicon)[ejsmont-artur](/maintainers/ejsmont-artur)

---

Top Contributors

[![ejsmont-artur](https://avatars.githubusercontent.com/u/345911?v=4)](https://github.com/ejsmont-artur "ejsmont-artur (36 commits)")

---

Tags

error handlingcircuit breakergracefulsymfony2 adapter

### Embed Badge

![Health badge](/badges/ejsmont-artur-php-circuit-breaker-bundle/health.svg)

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

###  Alternatives

[ackintosh/ganesha

PHP implementation of Circuit Breaker pattern

6613.9M13](/packages/ackintosh-ganesha)[ejsmont-artur/php-circuit-breaker

PHP Circuit Breaker component

169964.9k4](/packages/ejsmont-artur-php-circuit-breaker)[yiisoft/friendly-exception

An interface for friendlier exception

491.8M44](/packages/yiisoft-friendly-exception)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

3786.5k](/packages/harris21-laravel-fuse)[illuminated/console-logger

Logging and Notifications for Laravel Console Commands.

8674.9k](/packages/illuminated-console-logger)[lcobucci/error-handling-middleware

A PSR-15 middleware compatible with RFC 7807

6171.3k1](/packages/lcobucci-error-handling-middleware)

PHPackages © 2026

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