ejsmont-artur/php-circuit-breaker
Laravel-friendly PHP circuit breaker implementation to add resiliency to external service calls. Supports configurable failure thresholds, timeouts and recovery, helping prevent cascading failures when APIs or dependencies are down or slow.
A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).
It is a library providing extremely easy to use circuit breaker component. It does not require external dependencies and it has default storage implementations for APC and Memcached but can be extended multiple ways.
This library does not require any particular PHP framework, all you need is PHP 5.3 or higher.
If you are using Symfony 2 framework you should use php-circuit-breaker-bundle. It is a bundle i have created to wrap up php-circuit-breaker and integrate it with Symfony 2 components and Dependency Injection.
If you are using other frameworks and you would like to use php-circuit-breaker please let me know and we can try to build up an open source integration just like the one for Symfony 2.
You can download sources and use them with your autoloader or you can use composer in which case all you nees is a require like this:
"require": {
"ejsmont-artur/php-circuit-breaker": "*"
},
After that you should update composer dependencies and you are good to go.
Code of your application could look something like:
As you can see that is a very powerful concept of selectively disabling feautres at runtime but still allowing the core business processes to be uninterrupted.
Backend talking to the payment service could look like this:
Since you are recording failed and successful operations you can now use them in the front end as well to hide payment options that are failing.
Frontend rendering the available payment options could look like this:
Overhead of the Circuit Breaker is negligible.
APC implementation takes roughly 0.0002s to perform isAvailable() and then reportSuccess() or reportFailure().
Memcache adapter is in range of 0.0005s when talking to the local memcached process.
The only potential performance impact is network connection time. If you chose to use remote memcached server or implement your own custom StorageAdapter.
You can run all tests by any of the following:
ant
ant phpunit
ant ci
You can run selected test case by running:
cd tests
phpunit Unit/Ejsmont/CircuitBreaker/Storage/Adapter/DummyAdapterTest.php
Before documentation gets updated you can read more on the concept of circuit breaker and its implementation on my blog http://artur.ejsmont.org/blog/circuit-breaker
Some implementation details has changed but the core logic is still the same.
How can I help you explore Laravel packages today?