A modern PHP library that allows you to make resilient calls to external services :repeat:
This library is compatible with PHP 7.4+.
composer require love-oss/resiliency
You need to configure a system for the Circuit Breaker:
You'd better return the same type of response expected from your distant call.
use Resiliency\MainCircuitBreaker;
use Resiliency\Systems\MainSystem;
use Resiliency\Storages\SimpleArray;
use Resiliency\Clients\SymfonyClient;
use Symfony\Component\HttpClient\HttpClient;
$client = new SymfonyClient(HttpClient::create());
$mainSystem = MainSystem::createFromArray([
'failures' => 2,
'timeout' => 100,
'stripped_timeout' => 200,
'threshold' => 10000,
], $client);
$storage = new SimpleArray();
// Any PSR-14 Event Dispatcher implementation.
$dispatcher = new Symfony\Component\EventDispatcher\EventDispatcher;
$circuitBreaker = new MainCircuitBreaker(
$mainSystem,
$storage,
$dispatcher
);
/**
* @var Service $service
*/
$fallbackResponse = function ($service) {
return '{}';
};
$circuitBreaker->call(
'https://api.domain.com',
$fallbackResponse,
[
'query' => [
'_token' => '123456789',
]
]
);
Resiliency library supports both Guzzle (v6 & v7) and HttpClient Component from Symfony (v4 & v5).
This library provides a minimalist system to help you monitor your circuits.
$monitor = new SimpleMonitor();
// Collect information while listening
// to some circuit breaker events...
function listener(Event $event) {
$monitor->add($event);
};
// Retrieve a complete report for analysis or storage
$report = $monitor->getReport();
composer test
This library has high quality standards:
composer cs-fix && composer phpstan && composer psalm && composer phpqa
We also use PHPQA to check the Code quality during the CI management of the contributions:
composer phpqa
Welcome, that's an interesting question !
Above all, I must say that I'm the former author of the PrestaShop Circuit Breaker library and I have decided to fork my own library to be able to improve it without the constraints of the PrestaShop CMS main project.
As of now (June, 2021), these libraries have a lot in common !
They share almost the same API, and the PrestaShop Core Team have created multiple implementations of Circuit Breaker interface and Factory :
declare(strict_types=1);
on all the files ;.gitattributes
file, this means that all tests are downloaded when we require their library ;All right ... but this don't tell me what library should I use in my project !